I am trying to work with the np.longdouble
dtype in my Python code, and am trying to use NumPy to manipulate long doubles that I get from a C module compiled with Cython.
Suppose I do this:
import numpy as np
print np.finfo(np.longdouble)
Machine parameters for float128
---------------------------------------------------------------------
precision= 18 resolution= 1e-18
machep= -63 eps= 1.08420217249e-19
negep = -64 epsneg= 5.42101086243e-20
minexp=-16382 tiny= 3.36210314311e-4932
maxexp= 16384 max= 1.18973149536e+4932
nexp = 15 min= -max
---------------------------------------------------------------------
a = np.longdouble(1e+346)
a
Out[4]: inf
b = np.longdouble(1e+347)
b
Out[6]: inf
c = a/b
/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/start_ipython_kernel.py:1:
RuntimeWarning: invalid value encountered in longdouble_scalars
# -*- coding: utf-8 -*-
c
Out[8]: nan
a.dtype, b.dtype, c.dtype
Out[9]: (dtype('float128'), dtype('float128'), dtype('float128'))
In essence, it is linked to the same issue as in this question and I understand that Python first converts the 1e+346
into a float, whose represntation would be inf
. However, can someone suggest a workaround? Is there a way to create NumPy longdoubles that are not converted to floats first?
I have a C module that can output long doubles, which I want to use in a numpy array of dtype np.longdouble
.
Even if the solution involves re-compiling Python/NumPy, I am willing to try it.