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.Sun, 30 Nov 2014 21:50:26 +0100Why does Sage return negative number when evaluating 181.0%360https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/ sage: print 181%360 , 181.0%360
181 -179.000000000000
Sun, 23 Nov 2014 16:32:43 +0100https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/Answer by Luca for <pre><code> sage: print 181%360 , 181.0%360
181 -179.000000000000
</code></pre>
https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/?answer=24960#post-id-24960So that this code works
sage: a = 181.0
sage: b = 360
sage: n = (a / b).round()
sage: b * n + (a % b) == a
True
From the sage docstring (which you can read by typing `a.__mod__?`)
> Return the value of "left - n*right", rounded according to the
> rounding mode of the parent, where "n" is the integer quotient of
> "x" divided by "y". The integer "n" is rounded toward the nearest
> integer (ties rounded to even).
Sun, 23 Nov 2014 19:43:29 +0100https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/?answer=24960#post-id-24960Comment by ndomes for <p>So that this code works</p>
<pre><code>sage: a = 181.0
sage: b = 360
sage: n = (a / b).round()
sage: b * n + (a % b) == a
True
</code></pre>
<p>From the sage docstring (which you can read by typing <code>a.__mod__?</code>)</p>
<blockquote>
<p>Return the value of "left - n*right", rounded according to the
rounding mode of the parent, where "n" is the integer quotient of
"x" divided by "y". The integer "n" is rounded toward the nearest
integer (ties rounded to even).</p>
</blockquote>
https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/?comment=25017#post-id-25017My question wasn't HOW Sage evaluates 181.0%360, the question is WHY is it done this way. Pure Python returns 181 , Wolfram Alpha returns 181. when entering 181. mod 360 - Sage behaves different - what for?Thu, 27 Nov 2014 10:41:08 +0100https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/?comment=25017#post-id-25017Comment by Luca for <p>So that this code works</p>
<pre><code>sage: a = 181.0
sage: b = 360
sage: n = (a / b).round()
sage: b * n + (a % b) == a
True
</code></pre>
<p>From the sage docstring (which you can read by typing <code>a.__mod__?</code>)</p>
<blockquote>
<p>Return the value of "left - n*right", rounded according to the
rounding mode of the parent, where "n" is the integer quotient of
"x" divided by "y". The integer "n" is rounded toward the nearest
integer (ties rounded to even).</p>
</blockquote>
https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/?comment=25063#post-id-25063Exactly for the reason I said: so that the output is consistent with the output of `a/b`, the rounding mode, and the mathematical definition of Euclidean division (which in truth does not make much sense for floats).
In Python rounding is done to the lowest integer, so it makes sense to return 181, indeed
>>> 360 * int(181.0 / 360) + (181.0 % 360)
181.0
In Mathematica, rounding is done to the closest integer, and indeed
360*round(181.0 / 360) + (181.0 mod 360)
answers 541 on WolframAlpha. Oups! Guess Mathematica does not really care for mathematical consistency.Sun, 30 Nov 2014 21:50:26 +0100https://ask.sagemath.org/question/24959/why-does-sage-return-negative-number-when-evaluating-1810360/?comment=25063#post-id-25063