# Why 5.000000000000000 is not equal to 5

sage: def f(n):
...        if abs(n-n.integer_part())<1*e-10:
...            return n.integer_part()
...        else:
...            return n
sage: n=5.0000000000000001
sage: f(n)

5.000000000000000

sage: type(f(n))
<type 'sage.rings.real_mpfr.RealLiteral'>
sage: n.integer_part()
5
sage: n.integer_part()==5
True
sage: f(n)==5
False

edit retag close merge delete

Sort by » oldest newest most voted

The fact that f(n) shows 5.000000000000000 is just a printing artifact, there is no rounding, you can check that f(n) is equal to n and different from 5:

sage: n == f(n)
True


Even n itself is represented as 5.000000000000000, not 5.0000000000000001:

sage: n=5.0000000000000001
sage: n
5.000000000000000
sage: n-5
1.110223024625157e-16


EDIT Here is a way to print more digits of n

sage: n.str(truncate=False)
'5.000000000000000111'


You might be scary with the two additional 1 at the end. This is because internally, numbers are represented in binary, and you proposed a number which is exact in base 10, but not in base 2, hence some rounding (not only in the printing, but also in the representation of n in the machine).

You can get the exact value of the rounded n as follows:

sage: n.exact_rational()
45035996273704961/9007199254740992


And even see its internal representation:

sage: n.sign_mantissa_exponent()
(1, 90071992547409922, -54)


And check

sage: 90071992547409922*2^(-54)
45035996273704961/9007199254740992


You can print the non-truncated expression of n as follows:

more

1

1

@tmonteil - actually, I think you could expand your answer to mention how to get more digits printed (if not by default) because I think that might be part of the question the OP has.

That is awesome - there are a few places this shows up in Sage documentation but it needs more visibility.