-1

I'm trying to divide a number by small floating point number and I got correct result but when I compare this result with another number then I's showing me error

temp_ans=7 / 0.00000000000000000000007
tt=1e+23
print tt,temp_ans,type(tt),type(temp_ans)
if tt==temp_ans:
    print "YES1"
if 1e+23==temp_ans:
    print "YES2"
if tt==1e+23:
    print "YES3"

output:
1e+23 1e+23 <type 'float'> <type 'float'>
YES3

expected output :
1e+23 1e+23 <type 'float'> <type 'float'>
YES1
YES2
YES3
Rahul Verma
  • 2,988
  • 2
  • 11
  • 26
  • When i try your division I get 1.0000000000000001e+23, so you might have a truncated value. If I find a way to avoid that I will post it as an answer – Alex_6 Aug 09 '19 at 10:48

2 Answers2

1

You are actually comparing two different values. Looks like there is some issue in Python2 vs Python3. May be it's because of change in how the division operator - / works in Python2.

Both are giving different values for temp_ans.

Python2: this is what I got.

7/0.00000000000000000000007 gave me 1e+23.

Python3: this is what I got.

7/0.00000000000000000000007 gave me 1.0000000000000001e+23.

Note that there is a digit 1 before e.

and you are comparing this with 1e+23 which literally translates to 1.00000000000000000000000e+23.

Note that there is NO - 1 before e here.

What you are expecting will be printed when the condition is like this.

1e+23 == 1.00000000000000000000000e+23.

But the actual condition it is checking is this:

1e+23 == 1.00000000000000000000001e+23.

Looks like Python2 is rounding off the value to 1e+23.

Hope it clarified your query.

Underoos
  • 4,708
  • 8
  • 42
  • 85
  • Why 7/0.00000000000000000000007 gave me 1.0000000000000001e+23. in python 3 – Rahul Verma Aug 09 '19 at 11:23
  • My guess - bcoz of change in division operator. – Underoos Aug 09 '19 at 11:24
  • 3
    There was no change in how division worked for floats (or mixed floats and ints) between Python 2 and Python 3. I get exactly the same results in Python 2.7 and Python 3.7. However, if you're `print`ing the results, you _will_ see a difference; that has nothing to do with division changes, and everything to do with changes in the `str` of a float between Python 2 and Python 3. – Mark Dickinson Aug 09 '19 at 11:36
  • Also note that Python does not fully specify floating-point semantics, so specific behaviors are up to individual Python implementations. – Eric Postpischil Aug 09 '19 at 12:12
  • @Mark Dickinson So any idea why we are getting false for that comparison? – Underoos Aug 09 '19 at 12:18
  • You're getting `False` (in both Python 2.7 and Python 3.x) because the two values you're comparing aren't equal. It's that simple. – Mark Dickinson Aug 09 '19 at 14:14
  • @MarkDickinson why they aren't equal, This is what I'm not getting. – Rahul Verma Aug 10 '19 at 05:50
  • @RahulVerma Are you aware of floating-point error? That is probably what is happening here. – iz_ Aug 10 '19 at 06:12
1

While your expectation seems to make perfect sense in our human decimal way of thinking, for a computer it does not. Computers think binary, and can only remember so-many bits. At the current time of writing, this is most likely 64 bits, and with 64 bits it is impossible to represent all possible numbers. Numbers such as 0.1 and 0.2 cannot be represented accurately, while 0.5 and 0.25 can. The same holds for the number 0.00000000000000000000007. It cannot be represented using 64 bits in the IEEE-754 standard.

The closest binary number, in IEEE-754 notation, of 0.00000000000000000000007 is

s exponent e  significant
0 01110110101 0101001001111111110011011000000100000101110000000111

which, as a fraction, is written down as

5954941421116423/85070591730234615865843651857942052864 = 5954941421116423 * 2^-126

You can approximate this in Python as:

>>> x=0.00000000000000000000007
>>> x.as_integer_ratio()
(5954941421116423, 85070591730234615865843651857942052864L)
>>> print("%104.103e" % (x))
6.9999999999999998699796799570811032833536179595168135385857706952383860965483108884654939174652099609375

So while you strongly believe that 7/7E-23 equal 1E23, you actually compute something different. Your computer evaluates

 (7/5954941421116423) * 2^126 \approx 1.00000000000000001857433143470E23

Interesting reads:

kvantour
  • 25,269
  • 4
  • 47
  • 72