learning statistics
learning statistics

Reputation: 81

Extract the value of mpmath.gamma in python

The output of mpmath.gamma in python function prints the values in this way like:

mpf('1.7780824344856125e-342')

How to extract the value or just printing the value like this:

y=1.7780824344856125e-342

if I use float(y) it gives zero.

Upvotes: 2

Views: 111

Answers (2)

Learning is a mess
Learning is a mess

Reputation: 8277

As explained by Tino D the issue is that 1.77808e-342 is smaller than the smallest positive double precision float (the default python floating number container). You can check it with the numpy utils:

np.finfo(np.float64).tiny
# 2.2250738585072014e-308

which explains why it gets rounded down to 0. There are no double numbers between 0 and ~1e-308 and your very tiny number sits in that interval. When you cast it to a double, it gets "rounded down" to 0, hence your observation.

Upvotes: 1

Tino D
Tino D

Reputation: 2638

For me this is working fine:

from mpmath import mp
mp.dps = 10 # not really necessary...
smallValue = mp.mpf('1.7780824344856125e-342')
print(f"As a mp.mpf value: {smallValue}")
smallValueStr = mp.nstr(smallValue)
print(f"As a string: "+smallValueStr)

Results:

As a mp.mpf value: 1.778082434e-342
As a string: 1.77808e-342

Now I am not sure if you can represent this kind of value as a floating point. It has to do with the limits of these data types. For more reference, check this: https://en.wikipedia.org/wiki/IEEE_754

Upvotes: 1

Related Questions