The problem lies in your printing of the np.longdouble
. When you format using %f
, Python casts the result to a float (64-bits) before printing.
Here:
>>> a_int = np.longdouble(55197)
>>> a_float = np.longdouble(76601852) / 10**11
>>> b = a_int + a_float
>>> '%.25f' % b
'55197.0007660185219720005989075'
>>> '%.25f' % float(b)
'55197.0007660185219720005989075'
>>> b * 10**18
5.5197000766018519998e+22
Note that on my machine, I only get a bit more precision with longdouble
compared with ordinary double
(20 decimal places instead of 15). So, it may be worth seeing if the Decimal
module might be more suited for your application. Decimal
handles arbitrary-precision decimal floating-point numbers with no loss of precision.