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.Sat, 21 May 2016 23:17:36 +0200Why is sage behaving weird when rounding rational numbers?https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/I might be doing something stupid, but Sage's `int()` and `round()` functions seem to be buggy to me.
I'm trying to use these with rational numbers to round towards zero. I'd expect `-7/4` to round to `-1` and `+7/4` to round to `1`. Instead I get different behavior for rationals and their decimal representation:
┌────────────────────────────────────────────────────────────────────┐
│ SageMath Version 7.1, Release Date: 2016-03-20 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: int(-1.75) # Correctly rounds towards zero
-1
sage: int(-7/4) # Does not round towards zero
-2
sage: (-7/4).round("toward") # Does not round towards zero (despite what the documentation claims!)
-2
sage: -7/4 + 0.0 # The numbers are in fact the same!
-1.75
How do I round rational numbers towards zero in Sage?Sat, 21 May 2016 14:34:27 +0200https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/Comment by calc314 for <p>I might be doing something stupid, but Sage's <code>int()</code> and <code>round()</code> functions seem to be buggy to me. </p>
<p>I'm trying to use these with rational numbers to round towards zero. I'd expect <code>-7/4</code> to round to <code>-1</code> and <code>+7/4</code> to round to <code>1</code>. Instead I get different behavior for rationals and their decimal representation:</p>
<pre><code>┌────────────────────────────────────────────────────────────────────┐
│ SageMath Version 7.1, Release Date: 2016-03-20 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: int(-1.75) # Correctly rounds towards zero
-1
sage: int(-7/4) # Does not round towards zero
-2
sage: (-7/4).round("toward") # Does not round towards zero (despite what the documentation claims!)
-2
sage: -7/4 + 0.0 # The numbers are in fact the same!
-1.75
</code></pre>
<p>How do I round rational numbers towards zero in Sage?</p>
https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/?comment=33496#post-id-33496Also, you may wish to use `ceil` or `floor` for your application.Sat, 21 May 2016 19:30:21 +0200https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/?comment=33496#post-id-33496Comment by FrédéricC for <p>I might be doing something stupid, but Sage's <code>int()</code> and <code>round()</code> functions seem to be buggy to me. </p>
<p>I'm trying to use these with rational numbers to round towards zero. I'd expect <code>-7/4</code> to round to <code>-1</code> and <code>+7/4</code> to round to <code>1</code>. Instead I get different behavior for rationals and their decimal representation:</p>
<pre><code>┌────────────────────────────────────────────────────────────────────┐
│ SageMath Version 7.1, Release Date: 2016-03-20 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: int(-1.75) # Correctly rounds towards zero
-1
sage: int(-7/4) # Does not round towards zero
-2
sage: (-7/4).round("toward") # Does not round towards zero (despite what the documentation claims!)
-2
sage: -7/4 + 0.0 # The numbers are in fact the same!
-1.75
</code></pre>
<p>How do I round rational numbers towards zero in Sage?</p>
https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/?comment=33495#post-id-33495Try to read
t=3/2
t.round?Sat, 21 May 2016 19:02:08 +0200https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/?comment=33495#post-id-33495Answer by paulmasson for <p>I might be doing something stupid, but Sage's <code>int()</code> and <code>round()</code> functions seem to be buggy to me. </p>
<p>I'm trying to use these with rational numbers to round towards zero. I'd expect <code>-7/4</code> to round to <code>-1</code> and <code>+7/4</code> to round to <code>1</code>. Instead I get different behavior for rationals and their decimal representation:</p>
<pre><code>┌────────────────────────────────────────────────────────────────────┐
│ SageMath Version 7.1, Release Date: 2016-03-20 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: int(-1.75) # Correctly rounds towards zero
-1
sage: int(-7/4) # Does not round towards zero
-2
sage: (-7/4).round("toward") # Does not round towards zero (despite what the documentation claims!)
-2
sage: -7/4 + 0.0 # The numbers are in fact the same!
-1.75
</code></pre>
<p>How do I round rational numbers towards zero in Sage?</p>
https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/?answer=33498#post-id-33498I just tested `int(-7/4)` on [sagecell.sagemath.org](http://sagecell.sagemath.org/?q=wkgsqs) and it returns `-1` as expected. This server is now running Sage 7.2, so perhaps there is an issue in 7.1.
The `.round()` option only applies to half integers, as indicated in the [documentation](http://doc.sagemath.org/html/en/reference/rings_standard/sage/rings/rational.html#sage.rings.rational.Rational.round). For example, `(-3/2).round("toward")` returns `-1` while `(-3/2).round("away")` returns `-2`.
As suggested by @calc314, here is a function that rounds toward zero:
def f(x):
if x > 0:
return floor(x)
else:
return ceil(x)Sat, 21 May 2016 23:17:36 +0200https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/?answer=33498#post-id-33498