3

this is the code.

import math

a = math.pow(10,100)
b = 10 ** 100
c = 10 ** 100

print(a==b)   # false
print(a-b==0) # true

I know the math.pow is not correct while the number is too large. but how to understand the next point that a == b is not the same as a-b == 0 ?

my python version is 3.9.6 64bit

pythonnoob
  • 69
  • 2
  • ```math.pow``` returns ```1e+100```. And ```10**100``` returns ```1000......``` –  Aug 05 '21 at 13:28
  • 1
    [When does Python perform type conversion when comparing int and float?](https://stackoverflow.com/q/52557054) – 001 Aug 05 '21 at 13:52

4 Answers4

1

This is because of floatingpoint error that can happen to large or small float number. See more info on https://docs.python.org/3/tutorial/floatingpoint.html

Float numbers is stored as an 53 bit. So this problem will not happen to float numbers lower than 4503599627370496. See https://en.wikipedia.org/wiki/IEEE_754-1985 for more info.

import math

a = math.pow(10,100)
b = 10 ** 100

print(type(a))  # float
print(type(b))  # int

We can see that a is an float and b is an int.

print(int(a)) # 10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104

If we convert a into an int we will see that the number has changed and is no longer math.pow(10,100)

print(a==b)  # false
print(int(a)==int(b))   # false
print(float(a)==float(b))   # true
print(a-b==0) # true

If we try to convert both to int it will be the same as a==b but if we convert both to float it will work. That is because both numbers will get the same conversion error.

When we do print(a-b==0) this will be converted correctly.

Sefan
  • 699
  • 1
  • 8
  • 23
0
math.pow, value type is float
a = math.pow(10,100)
b = float(10 ** 100)
a==b is true
RGB_DS
  • 98
  • 4
0

For large numbers, the precision is low that is why one is True, the other is False, if you try with a smaller number, you will see that both will evaluate to be True:

>>> math.pow(10,15)
1000000000000000.0
>>> 10**15
1000000000000000
>>> math.pow(10,15)==10**15
True

>>> math.pow(10,20)
1e+20
>>> 10**20
100000000000000000000
>>> math.pow(10,20) == 10**20
True

But if you look at some larger numbers represented by exponent:

>>> math.pow(10,50)
1e+50
>>>10**50
100000000000000000000000000000000000000000000000000
>>> math.pow(10,50)==10**50
False

If you try to convert it to integer, you'll see that the number just changes:

>>> int(math.pow(10,50))
100000000000000007629769841091887003294964970946560
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
0

For larger numbers (like you've used math.pow(10, 100)), a-b == 0 returns False because of Floating point arithmetic.

math.pow() - Converts its arguments to float type

From the Docs:

Unlike the built-in ** operator, math.pow() converts both its arguments to type float.

** and pow() - Returns an Integer.

Use ** or the built-in pow() function for computing exact integer powers.

import math
a = 10**2
b = math.pow(10,2)
c = pow(10,2)

print(f'a: {a}\nb: {b}\nc: {c}')
print(a==b)
print(a-b==0)
a: 100
b: 100.0
c: 100
True
True

From above results, you can see what types they return.

Also a-b == 0 returns True in this case since the numbers are small.

Ram
  • 4,724
  • 2
  • 14
  • 22