I am using python and have something like this-
a=3.472556691305291e-97
b=2.0842803001689662e-120
c=a/(a+b)
print(c)
I am getting value=1.0
. But I want the exact answer.Is there some way I can improve my accuracy here?
I am using python and have something like this-
a=3.472556691305291e-97
b=2.0842803001689662e-120
c=a/(a+b)
print(c)
I am getting value=1.0
. But I want the exact answer.Is there some way I can improve my accuracy here?
You can use an external library, such as mpmath, to get arbitrary precision floating point numbers.
Use the mpf
type for the numbers, as shown in the examples in the documentation:
>>> mpf(4)
mpf('4.0')
>>> mpf(2.5)
mpf('2.5')
>>> mpf("1.25e6")
mpf('1250000.0')
>>> mpf(mpf(2))
mpf('2.0')
>>> mpf("inf")
mpf('+inf')