Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回负值的sage中的模_Python_Sage - Fatal编程技术网

Python 返回负值的sage中的模

Python 返回负值的sage中的模,python,sage,Python,Sage,我刚接触SAGE,对一些非常简单的事情有点问题。我有以下代码: delay = float(3.5) D = delay%1.0 D 但这将返回值-0.5,而不是预期的0.5。我做错了什么 如果我将延迟更改为delay=float(2.5),我会得到正确的答案,因此我不知道为什么它不一致(我肯定我使用的模是错误的)。我认为这确实会很好地回答您的问题 但是,我不知道为什么在Sage中使用float。然后你可以直接使用Python。无论如何,%运算符在整数之外很难使用。例如,这里是用于Sage有理

我刚接触SAGE,对一些非常简单的事情有点问题。我有以下代码:

delay = float(3.5)
D = delay%1.0
D
但这将返回值
-0.5
,而不是预期的
0.5
。我做错了什么

如果我将延迟更改为
delay=float(2.5)
,我会得到正确的答案,因此我不知道为什么它不一致(我肯定我使用的模是错误的)。

我认为这确实会很好地回答您的问题

但是,我不知道为什么在Sage中使用
float
。然后你可以直接使用Python。无论如何,
%
运算符在整数之外很难使用。例如,这里是用于Sage有理数的docstring

   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

我认为这被认为是一个功能,而不是一个bug。

谢谢,这是一篇很好的文章,并解释了它。因为我只是想得到值的小数部分,所以我将代码的这一部分更改为
D=float(str(delay int(delay))[1:])
。另外,为了使模运算正常工作,我将其作为浮动进行了类型转换,从那时起,我已经删除了它们。