2014-09-19 85 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