1

So this just wasted a couple of hours of mine in debugging..

a,b = 32980.642295,32630.642295
print a-b
print int(a-b)
print int(a) - int(b)

Output :

350.0
349
350

Can someone explain to me what is going on? Why wouldn't the int of 350.0 just be 350? I assume python is doing some weird stuff in casting a and b before converting to int..

EDIT/ Update :

x = a-b

Value stored in x is now 349.99999999999636 not 350.0

This might help future people coming to the post Is floating point math broken?

Adit Sanghvi
  • 142
  • 1
  • 2
  • 12

4 Answers4

3

print implicitly applies str() to the things it's asked to print, and in Python 2 str(some_float) rounds to 12 significant digits. repr(some_float) rounds to 17:

>>> a,b = 32980.642295,32630.642295
>>> print str(a-b)
350.0
>>> print repr(a-b)
349.99999999999636

So that's why. a-b is actually a little less than 350.

In Python 3, str(some_float) and repr(some_float) both produce the shortest string s such that eval(s) == some_float:

# Python 3 here, not Python 2
>>> a,b = 32980.642295,32630.642295
>>> print(str(a-b))
349.99999999999636
>>> print(repr(a-b))
349.99999999999636
>>> eval(str(a-b)) == a-b
True
Tim Peters
  • 67,464
  • 13
  • 126
  • 132
  • I googled around and found out what ( I think) is the issue. It's not print for me, as I was storing the value in a variable and accessing it later, but its the way floating point math is handled. I just used print for the example here, my bad. This helped my understanding https://stackoverflow.com/questions/588004/is-floating-point-math-broken# If I make x = a-b and then check x, it will give me 349.99999999999636 – Adit Sanghvi Aug 25 '17 at 17:32
  • 2
    It makes no difference at all whether you use `a-b` directly or store it in a variable first. The computed value is exactly the same either way. The reason your output was `350.0` is as already explained: `print` in Python 2 _rounds_ the displayed value to 12 significant digits. It doesn't matter one whit whether you do `print a-b` or `print x`. – Tim Peters Aug 25 '17 at 17:40
2

print rounds floats more aggressively than you might expect on Python 2. a-b is very slightly lower than 350; print rounds this to 350, but int truncates the number to 349.

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

well, it is my understanding that in the first case your result if a float, which means it print the .0;

in the second case, it does the subtraction operation with float numbers, and than rounds it to the nearest integer;

in the third case, it rounds both values to the nearest integet, and than does the subtraction;

hope it was helpful

Rafael Marques
  • 1,335
  • 4
  • 22
  • 35
0

Floating math isn't broken. Due to conversion to (machine internal) binary representation, small errors are introduced depending on the size of internal number format. Thats the reason the result is a bit smaller than 350 as we calculate from the decimal numbers.

In the first line that result is rounded to 350, in second line the int() just truncates to 349.

The third line behaves differently and delivers by chance the result 350 because truncation changes both values in the same direction.

Juergen
  • 69
  • 5