[Numpy-discussion] longdouble (float96) literals

Colin Macdonald macdonald@maths.ox.ac...
Wed Aug 18 04:46:27 CDT 2010


How can I enter longdouble (float96) literals into my python/numpy 
programs?  In C, I would postfix such numbers with 'L', but this gives 
a SyntaxError in python.

The rest of my message is just two examples of what I'm talking about 
in case its not clear.

Thanks,
Colin

----------------------------------------------------------

Note:
longdouble: machine epsilon of approx 1e-19
float64: mach eps approx 1e-16

For example, say I need a longdouble accurate 0.4 in my code:

In [249]: f96 = numpy.longdouble
In [250]: f96(0.4) * f96(10) - f96(4)
Out[250]: 2.2204460492503130808e-16
# no, that converted 0.4 to a float64 first (and note the answer is 
machine epsilon for float64)
In [251]: f96("0.4") * f96(10) - f96(4)
Out[251]: 2.2204460492503130808e-16
# no
In [252]: (f96(4)/f96(10)) * f96(10) - f96(4)
Out[252]: 0.0
# this works (in this particular case).  I also would've been happy 
with 1e-19 in the last case---of course I don't expect it to be 
exactly zero.

Finally, here's another example involving pi, done without ipython:

import numpy as np
f96 = np.longdouble
# of course this won't work because numpy.pi is a float64
mypi0 = f96(np.pi)
print np.sin(mypi0)
# in C, I would write:
#mypi1 = 3.141592653589793238462643383279L
mypi1 = 3.141592653589793238462643383279
print np.sin(mypi1)
mypi2 = np.arctan2( f96(0), f96(-1) )
print np.sin(mypi2)

OUTPUT:
$ python f96_issue.py
1.22460635382e-16
1.22460635382e-16
-5.42101086243e-20

-- 
Colin Macdonald
University Lecturer in Numerical Methodologies
Tutorial Fellow at Oriel College
University of Oxford


More information about the NumPy-Discussion mailing list