[Numpy-discussion] longdouble (float96) literals

Charles R Harris charlesr.harris at gmail.com
Wed Aug 18 08:43:35 EDT 2010


On Wed, Aug 18, 2010 at 3:46 AM, Colin Macdonald
<macdonald at maths.ox.ac.uk>wrote:

> 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
>
>
I don't know how to do this without using strings and having numpy do it's
own string conversions. IIRC, we currently rely on Python for this and
Python doesn't support long doubles. You might open a ticket with a request
for an enhancement as we should fix this at some point.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100818/aac5c77f/attachment.html>


More information about the NumPy-Discussion mailing list