Python division doesn't work as expected for large numbers

118 Views Asked by At

I have three variables a, b and c. I want to make sure that after doing this:

c -= a*b

c is not negative.

I am doing this for now:

if a*b > c:
      b = c // a

but for very large numbers, I still get negative value in c. what should I do?

To reproduce the error here are some sample numbers:

a = 5.195497498518083

b = 1.0813434626413702e+16

c = 5.614533816817397e+16

1

There are 1 best solutions below

3
Martin On

As stated in the comments, this might not be related to the how large the numbers are, but to the fact you are working with float, and there are limitations. To illustrate the problem, check this in your REPL.

>>> a=0.1+0.2
>>> b=0.3
>>> a==b
False
>>> a
0.30000000000000004
>>> 

But you could try and work with integers as suggested by @rioV8