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
On Wed, Aug 18, 2010 at 3:46 AM, Colin Macdonald <macdonald@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
On 08/18/10 13:43, Charles R Harris wrote:
On Wed, Aug 18, 2010 at 3:46 AM, Colin Macdonald <macdonald@maths.ox.ac.uk <mailto:macdonald@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.
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.
I'd be at least partially satisfied with doing a string conversion ;-) How would I do that? Is there something like strtolongdouble()? Or a sscanf function? I will try to remember to open a ticket for the issue. Perhaps the issue that numpy.longdouble("0.4") doesn't work properly a slightly separate bug...
On Wed, Aug 18, 2010 at 7:37 AM, Colin Macdonald <macdonald@maths.ox.ac.uk>wrote:
On 08/18/10 13:43, Charles R Harris wrote:
On Wed, Aug 18, 2010 at 3:46 AM, Colin Macdonald <macdonald@maths.ox.ac.uk <mailto:macdonald@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.
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.
I'd be at least partially satisfied with doing a string conversion ;-) How would I do that? Is there something like strtolongdouble()? Or a sscanf function?
If you are willing to work at the C-Level you can probably use sscanf with the proper format specifier. Or you might write a parser in python. Numpy just doesn't have support for this at the moment, at least not that I am aware of. However, the various constants supplied by numpy, pi and such, are full precision.
I will try to remember to open a ticket for the issue.
Perhaps the issue that numpy.longdouble("0.4") doesn't work properly a slightly separate bug...
I'm pretty sure it goes through a double and can't be fixed until there is long double support for string conversions. Unicode makes this even trickier as to use the C-library it needs to be converted to ascii first. Chuck
On Wed, Aug 18, 2010 at 8:25 AM, Colin Macdonald <macdonald@maths.ox.ac.uk>wrote:
On 08/18/10 15:14, Charles R Harris wrote:
However, the various constants supplied by numpy, pi and such, are full precision.
no, they are not. My example demonstrated that numpy.pi is only double precision.
Hmm, the full precision values are available internally but it looks like they aren't available to the public. I wonder what the easiest way to provide them would be? Maybe they should be long double types by default? Chuck
On Wed, Aug 18, 2010 at 10:36 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Wed, Aug 18, 2010 at 8:25 AM, Colin Macdonald <macdonald@maths.ox.ac.uk> wrote:
On 08/18/10 15:14, Charles R Harris wrote:
However, the various constants supplied by numpy, pi and such, are full precision.
no, they are not. My example demonstrated that numpy.pi is only double precision.
Hmm, the full precision values are available internally but it looks like they aren't available to the public. I wonder what the easiest way to provide them would be? Maybe they should be long double types by default?
playing with some examples, I don't seem to be able to do anything with longdouble on win32, py2.5
np.array([3141592653589793238L], np.int64).astype(np.longdouble)[0] 3141592653589793300.0 np.array([3141592653589793238L], np.int64).astype(float)[0] 3.1415926535897933e+018 1./np.array([np.pi],np.longdouble)[0] - 1/np.pi 0.0 1./np.array([np.pi],np.longdouble)[0] 0.31830988618379069
and it doesn't look like it's the print precision
1./np.array([np.pi],np.longdouble)[0]*1e18 318309886183790720.0 1./np.array([np.pi],float)[0]*1e18 3.1830988618379072e+017
type conversion and calculations seem to go through float Josef
Chuck
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
I don't think there is longdouble on Windows, is there? Matthieu 2010/8/18 <josef.pktd@gmail.com>:
On Wed, Aug 18, 2010 at 10:36 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Wed, Aug 18, 2010 at 8:25 AM, Colin Macdonald <macdonald@maths.ox.ac.uk> wrote:
On 08/18/10 15:14, Charles R Harris wrote:
However, the various constants supplied by numpy, pi and such, are full precision.
no, they are not. My example demonstrated that numpy.pi is only double precision.
Hmm, the full precision values are available internally but it looks like they aren't available to the public. I wonder what the easiest way to provide them would be? Maybe they should be long double types by default?
playing with some examples, I don't seem to be able to do anything with longdouble on win32, py2.5
np.array([3141592653589793238L], np.int64).astype(np.longdouble)[0] 3141592653589793300.0 np.array([3141592653589793238L], np.int64).astype(float)[0] 3.1415926535897933e+018 1./np.array([np.pi],np.longdouble)[0] - 1/np.pi 0.0 1./np.array([np.pi],np.longdouble)[0] 0.31830988618379069
and it doesn't look like it's the print precision
1./np.array([np.pi],np.longdouble)[0]*1e18 318309886183790720.0 1./np.array([np.pi],float)[0]*1e18 3.1830988618379072e+017
type conversion and calculations seem to go through float
Josef
Chuck
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Information System Engineer, Ph.D. Blog: http://matt.eifelle.com LinkedIn: http://www.linkedin.com/in/matthieubrucher
On Wed, Aug 18, 2010 at 8:54 AM, Matthieu Brucher < matthieu.brucher@gmail.com> wrote:
I don't think there is longdouble on Windows, is there?
The MSVC compilers don't support extended precision, or rather, long doubles are the same as doubles. I don't know what other compilers on windows do. <snip> Chuck
On Wed, Aug 18, 2010 at 10:58 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Wed, Aug 18, 2010 at 8:54 AM, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
I don't think there is longdouble on Windows, is there?
np.longdouble <type 'numpy.float96'>
np.longdouble(5).itemsize 12 np.double(5).itemsize 8
The MSVC compilers don't support extended precision, or rather, long doubles are the same as doubles. I don't know what other compilers on windows do.
I have the mingw version, gcc3.5 or so So to me it looks like float96 is there, but not used in calculations. (I never tried to use it for anything before.) Josef
<snip>
Chuck
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Wed, Aug 18, 2010 at 11:52 PM, <josef.pktd@gmail.com> wrote:
On Wed, Aug 18, 2010 at 10:36 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Wed, Aug 18, 2010 at 8:25 AM, Colin Macdonald <macdonald@maths.ox.ac.uk> wrote:
On 08/18/10 15:14, Charles R Harris wrote:
However, the various constants supplied by numpy, pi and such, are full precision.
no, they are not. My example demonstrated that numpy.pi is only double precision.
Hmm, the full precision values are available internally but it looks like they aren't available to the public. I wonder what the easiest way to provide them would be? Maybe they should be long double types by default?
playing with some examples, I don't seem to be able to do anything with longdouble on win32, py2.5
For all practical purposes, double == long double on windows: although some compilers support the intel long format or even quadd precision, the MSVC runtime does not (so you cannot print, scan, etc... making them useless). cheers, David
np.array([3141592653589793238L], np.int64).astype(np.longdouble)[0] 3141592653589793300.0 np.array([3141592653589793238L], np.int64).astype(float)[0] 3.1415926535897933e+018 1./np.array([np.pi],np.longdouble)[0] - 1/np.pi 0.0 1./np.array([np.pi],np.longdouble)[0] 0.31830988618379069
and it doesn't look like it's the print precision
1./np.array([np.pi],np.longdouble)[0]*1e18 318309886183790720.0 1./np.array([np.pi],float)[0]*1e18 3.1830988618379072e+017
type conversion and calculations seem to go through float
Josef
Chuck
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Wed, Aug 18, 2010 at 11:07 AM, David Cournapeau <cournape@gmail.com> wrote:
On Wed, Aug 18, 2010 at 11:52 PM, <josef.pktd@gmail.com> wrote:
On Wed, Aug 18, 2010 at 10:36 AM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Wed, Aug 18, 2010 at 8:25 AM, Colin Macdonald <macdonald@maths.ox.ac.uk> wrote:
On 08/18/10 15:14, Charles R Harris wrote:
However, the various constants supplied by numpy, pi and such, are full precision.
no, they are not. My example demonstrated that numpy.pi is only double precision.
Hmm, the full precision values are available internally but it looks like they aren't available to the public. I wonder what the easiest way to provide them would be? Maybe they should be long double types by default?
playing with some examples, I don't seem to be able to do anything with longdouble on win32, py2.5
For all practical purposes, double == long double on windows: although some compilers support the intel long format or even quadd precision, the MSVC runtime does not (so you cannot print, scan, etc... making them useless).
Thanks for the info, I can stop playing Josef
cheers,
David
np.array([3141592653589793238L], np.int64).astype(np.longdouble)[0] 3141592653589793300.0 np.array([3141592653589793238L], np.int64).astype(float)[0] 3.1415926535897933e+018 1./np.array([np.pi],np.longdouble)[0] - 1/np.pi 0.0 1./np.array([np.pi],np.longdouble)[0] 0.31830988618379069
and it doesn't look like it's the print precision
1./np.array([np.pi],np.longdouble)[0]*1e18 318309886183790720.0 1./np.array([np.pi],float)[0]*1e18 3.1830988618379072e+017
type conversion and calculations seem to go through float
Josef
Chuck
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (5)
-
Charles R Harris -
Colin Macdonald -
David Cournapeau -
josef.pktd@gmail.com -
Matthieu Brucher