ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 22 Dec 2015 00:34:45 +0100Why (11+2/3)%2=1 and (11+1/3)%2=0?https://ask.sagemath.org/question/31740/why-112321-and-111320/I did not expect this behavior...
For instance, I would expect an error message or a warning.
Is there an explanation?Mon, 21 Dec 2015 22:43:37 +0100https://ask.sagemath.org/question/31740/why-112321-and-111320/Answer by vdelecroix for <p>I did not expect this behavior...
For instance, I would expect an error message or a warning.</p>
<p>Is there an explanation?</p>
https://ask.sagemath.org/question/31740/why-112321-and-111320/?answer=31741#post-id-31741Each Sage object might behave differently with respect to the modulo operator %. In your situation this is due to the implementation of this operator for rational numbers. Namely x % y if x is rational and y integer Sage will do the following
def modulo_for_rational(x,y):
x = ZZ(x)
n = x.numerator() % y
d = x.denominator() % y
d = d.inverse_mod(y)
return (n * d) % y
Note that all the modulo in the above code are between integers. Hence the rationale is: in x % y with x a rational and y an integer should be considered as a computation in ZZ / y ZZ.
This is indeed inconsistent with the behavior of % when x is a floating point number... where the definition is of x % y is the unique real number in [-y/2, y/2) of the form x + n y with n integer.
sage: 3.2 % 1
0.200000000000000
sage: 3.7 % 1
-0.300000000000000
PS: you can look at the complete source code of the modulo operator with
sage: Rational.__mod__??Tue, 22 Dec 2015 00:20:53 +0100https://ask.sagemath.org/question/31740/why-112321-and-111320/?answer=31741#post-id-31741Comment by vdelecroix for <p>Each Sage object might behave differently with respect to the modulo operator %. In your situation this is due to the implementation of this operator for rational numbers. Namely x % y if x is rational and y integer Sage will do the following</p>
<pre><code>def modulo_for_rational(x,y):
x = ZZ(x)
n = x.numerator() % y
d = x.denominator() % y
d = d.inverse_mod(y)
return (n * d) % y
</code></pre>
<p>Note that all the modulo in the above code are between integers. Hence the rationale is: in x % y with x a rational and y an integer should be considered as a computation in ZZ / y ZZ.</p>
<p>This is indeed inconsistent with the behavior of % when x is a floating point number... where the definition is of x % y is the unique real number in [-y/2, y/2) of the form x + n y with n integer.</p>
<pre><code>sage: 3.2 % 1
0.200000000000000
sage: 3.7 % 1
-0.300000000000000
</code></pre>
<p>PS: you can look at the complete source code of the modulo operator with</p>
<pre><code>sage: Rational.__mod__??
</code></pre>
https://ask.sagemath.org/question/31740/why-112321-and-111320/?comment=31743#post-id-31743see also https://groups.google.com/forum/#!topic/sage-devel/PfMop0nyiL0Tue, 22 Dec 2015 00:34:45 +0100https://ask.sagemath.org/question/31740/why-112321-and-111320/?comment=31743#post-id-31743