0

I am newbie to python. I have one surprise result after executing following mathematical operation:

1.1 + 2.2 

= 3.3000000000000003

then 4 / (2.0 + 3) = should be 0.80000000000000004 as per above operation.

but python return 0.8.

i am not understanding why this difference is? enter image description here

i am using Python 2.7.6 Please see attached image.

i know that if i use print (1/3.0) then it will give 3.30

My question are 1. why all digits of second operation is not displayed but all digit of first one's displayed?

  1. how can we compare floating variables those values are dynamic for accurate result?
Vishal Patel
  • 1,715
  • 16
  • 26
  • [What every computer scientist should know about floating-point arithmetic.](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – cat Sep 23 '16 at 13:49
  • And its not only python, try Java and check the results. – Techidiot Sep 23 '16 at 13:50
  • I recall that Python does do something funky with its dealings with floating point. I can't recall what they call it though. I've retracted my dupe close vote since I think it could be important. – Bathsheba Sep 23 '16 at 13:51
  • Voted to reopen. The dupe only partially explains Python's output. – Bathsheba Sep 23 '16 at 13:53
  • 1
    `1 / 3.0` seriously gives you `3.30`? Interesting Python you have there :) – Jon Clements Sep 23 '16 at 13:53
  • @MarkDickinson: That's the *exact* question I was thinking about: "round-tripping" was the term I was trying to recall. – Bathsheba Sep 23 '16 at 13:58
  • @JonClements :i cant understand what are you want to say? is my screen are wrong or something else? – Vishal Patel Sep 24 '16 at 06:05

1 Answers1

1

Python uses a minimalist repr for floats when possible. That is, it prints a value that is as precise as necessary to reproduce the exact value of the original float if you typed it in directly.

For 1.1 + 2.2, the imprecision means it doesn't actually produce 3.3 precisely (1.1 + 2.2 == 3.3 will evaluate to False). By contrast, 4 / (2.0 + 3) is exact enough that 0.8 represents it precisely, (4 / (2.0 + 3) == 0.8 evaluates to True) so it doesn't include a whole bunch of 0s that aren't required to reproduce the value.

If you want to explicitly print a certain number of places after the decimal, use str formatting to specify, e.g.:

>>> print('{:.16f}'.format(4 / (2.0 + 3)))
0.8000000000000000

If you formatted it with additional digits, you'd see the 4, but that value is still exactly the same as 0.8; 0.80000000000000004 == 0.8 evaluates to True, so Python avoids the verbosity and just gives you shortest of many equivalent representations.

The What's new in Python 3.1 docs (scroll to end of linked section, just before "New, Improved and Deprecated Modules") are a useful explanation for why/when Python 2.7/3.1+ have much shorter float reprs for some values. Straight from the horse's mouth, so to speak.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • i have updated my question and i dont want to print but i want to know logic behind it and how to compare these type floating values? – Vishal Patel Sep 24 '16 at 06:45