-1

i got probability extremely weak and i need divide 1 proba per 1 other probability.

Imagine it :

a=5e-150000
b=a=5e-150000
print(a/b)

the results must be 1 but python say me :

    print(a/b)
ZeroDivisionError: float division by zero

Do you have a solution to don't got this message error ?

Thanks for reading me !

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
léo
  • 5
  • 3

1 Answers1

3

The floating-point type cannot represent such a value:

>>> 5e-150000
0.0

You can instead use the Decimal class from the standard library to represent numbers like that:

from decimal import Decimal
Decimal('5e-150000') * Decimal('2e149999') # evaluates to `Decimal(1.0)`
Decimal('5e-150000') / Decimal('5e-150000') # no problem here either
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    It could be noted that you can check if a number is too small for a float representation; if it's below the MIN value gotten via the method outlined [here](https://stackoverflow.com/questions/1835787/what-is-the-range-of-values-a-float-can-have-in-python). – Random Davis Sep 23 '20 at 22:21