2014-09-19 48 views
0

我是SAGE的新手,並且遇到非常簡單的問題。我有以下代碼:Modulo in sage returns a negative value

delay = float(3.5) 
D = delay%1.0 
D 

但這返回值-0.5而不是預期的0.5的。我究竟做錯了什麼?

如果我改變延遲爲delay = float(2.5),我得到了正確的答案,所以我不知道爲什麼它不一致(我確信我以某種方式使用模錯)。

回答

2

我認爲this question會爲你做得很好。

但是,我不知道你爲什麼在Sage中使用float。然後你可以直接使用Python。無論如何,%運算符在整數之外使用很困難。例如,下面是在Sage有理數上使用的文檔字符串。

Return the remainder of division of self by other, where other is 
    coerced to an integer 

    INPUT: 

    * ``other`` - object that coerces to an integer. 

    OUTPUT: integer 

    EXAMPLES: 

     sage: (-4/17).__mod__(3/1) 
     1 

我認爲這被認爲是一個功能,而不是一個錯誤。

+0

謝謝你,這是很好的閱讀,並解釋它。因爲我只是想獲取值的小數部分,所以我將這段代碼改爲'D = float(str(delay-int(delay))[1:])'。另外,我曾嘗試將模塊作爲浮動模塊來嘗試模塊化工作,因此我已將它們移除。 – toozie21 2014-09-19 12:41:15