Ask Your Question

con-f-use's profile - activity

2021-04-22 14:04:39 +0200 received badge  Famous Question (source)
2020-07-15 00:35:02 +0200 received badge  Notable Question (source)
2019-04-21 14:22:03 +0200 received badge  Popular Question (source)
2016-05-24 11:17:46 +0200 received badge  Scholar (source)
2016-05-24 11:17:23 +0200 received badge  Supporter (source)
2016-05-23 03:22:31 +0200 received badge  Student (source)
2016-05-21 18:10:06 +0200 asked a question 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?