How to get a Python long double literal? I have tried with
numpy.longdouble(1e309)
and
numpy.longdouble("1e309")
but both of them just return inf
. What would be the right way to do that?
[EDIT] An answer below says that long double is treated as double in some platform. It is not the case on my system. To show this, I have tried:
np.longdouble(2.0)**1029
on my system (Mac OS 10.11). It returns
5.7526180315594109047e+309
[EDIT2] As suggested, I just tried
np.finfo(np.longdouble)
which gives
finfo(resolution=1e-18,
min=-1.18973149536e+4932,
max=1.18973149536e+4932,
dtype=float128)
on my OS. For info, my numpy version is 1.10.1.
longdouble , that is, 80 bits on most x86 machines and 64 bits in standard Windows builds. Be warned that even if np. longdouble offers more precision than python float , it is easy to lose that extra precision, since python often forces values to pass through float .
In C and related programming languages, long double refers to a floating-point data type that is often more precise than double precision though the language standard only requires it to be at least as precise as double . As with C's other floating-point types, it may not necessarily map to an IEEE format.
Long Double in C++ In the above example, a long double variable (var1) is initialized. The sizeof() operator determines the size of the variable var1 in bytes. The output displays the size of the variable i.e. 12 bytes and its value i.e. 114260.
On some platforms long double
is essential the same as double
. From numpy documentation:
NPY_LONGDOUBLE
The enumeration value for a platform-specific floating point type which is at least as large as NPY_DOUBLE, but larger on many platforms.
Sometimes long double is a 80-bit float (but not 128 bit, as many people would expect). You may check with:
numpy.finfo(numpy.longdouble)
Consider the following answers as well:
You may also try this:
n = numpy.longdouble(1e300) * 1e9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With