473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

numpy: handling float('NaN') different in XP vs. Linux


I have a script:

from numpy import float
OutD=[]
v=['3','43','23.4','NaN','43']
OutD.append([float(i) for i in v[1]])
On linux:
Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
[john@andLinux analysis]$ python jnk.py
[[3.0, 43.0, 23.399999999999999, nan, 43.0]]

On XP:
Python 2.5 (r25:51908, Mar 9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\analysis>C:\Python25\python.exe jnk.py
Traceback (most recent call last):
File "jnk.py", line 4, in <module>
OutD.append([float(i) for i in v])
ValueError: invalid literal for float(): NaN
WTF?
--
View this message in context: http://www.nabble.com/numpy%3A-handl...p17835502.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #1
6 2570
On Jun 13, 10:45*pm, "John [H2O]" <washa...@gmail.comwrote:
I have a script:

from numpy import float
OutD=[]
v=['3','43','23.4','NaN','43']
OutD.append([float(i) for i in v[1]])

On linux:
Python 2.5.1 (r251:54863, Mar *7 2008, 04:10:12)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
[john@andLinux analysis]$ python jnk.py
[[3.0, 43.0, 23.399999999999999, nan, 43.0]]

On XP:
Python 2.5 (r25:51908, Mar *9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\analysis>C:\Python25\python.exe jnk.py
Traceback (most recent call last):
* File "jnk.py", line 4, in <module>
* * OutD.append([float(i) for i in v])
ValueError: invalid literal for float(): NaN
Python just uses the atof() function from the underlying C library.
Some of them handle NaN's, and some of them don't.

If you want to get NaN on a platform where float('NaN') doesn't work,
try 1e1000 / 1e1000. Or failing that, struct.unpack('d',
struct.pack('Q', 0xfff8000000000000))[0]
Jun 27 '08 #2
On Jun 14, 1:45 pm, "John [H2O]" <washa...@gmail.comwrote:
I have a script:

from numpy import float
OutD=[]
v=['3','43','23.4','NaN','43']
OutD.append([float(i) for i in v[1]])

On linux:
Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
[john@andLinux analysis]$ python jnk.py
[[3.0, 43.0, 23.399999999999999, nan, 43.0]]

On XP:
Python 2.5 (r25:51908, Mar 9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\analysis>C:\Python25\python.exe jnk.py
Traceback (most recent call last):
File "jnk.py", line 4, in <module>
OutD.append([float(i) for i in v])
ValueError: invalid literal for float(): NaN

WTF?
Avoid impolite astonishment; RTFloatingM instead:
"""
float( [x])

Convert a string or a number to floating point. If the argument is a
string, it must contain a possibly signed decimal or floating point
number, possibly embedded in whitespace. Otherwise, the argument may
be a plain or long integer or a floating point number, and a floating
point number with the same value (within Python's floating point
precision) is returned. If no argument is given, returns 0.0.

Note: When passing in a string, values for NaN and Infinity may be
returned, depending on the underlying C library. The specific set of
strings accepted which cause these values to be returned depends
entirely on the C library and is known to vary.
"""

You may like to suggest a minor extension to the docs: after "is known
to vary" add "and may even be empty".

HTH,
John
Jun 27 '08 #3

John Machin wrote:
>

Avoid impolite astonishment; RTFloatingM instead:
"""

HTH,
John
--

I guess the key here is that it is not an issue with Python, but C... can I
change 'the underlying C code?' if so, WFM should I read for that!? :p
--
View this message in context: http://www.nabble.com/numpy%3A-handl...p17836073.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #4
On Jun 14, 3:33 pm, "John [H2O]" <washa...@gmail.comwrote:
John Machin wrote:
Avoid impolite astonishment; RTFloatingM instead:
"""
HTH,
John
--

I guess the key here is that it is not an issue with Python, but C... can I
change 'the underlying C code?'
The underlying C code for the Windows C RTL is probably on a server in
a bunker in Redmond WA ... good luck :-)

Perhaps you could start lashing up something along the lines that Dan
mentioned, e.g.

floated = {
'NaN': 1e1000 / 1e1000,
'Inf': whatever,
}.get

def myfloat(s):
try:
return float(s)
except:
value = floated(s)
if value is not None:
raise
return value

Then when/if your mapping has enough entries to make it worthwhile,
you could maybe suggest that this be done in Numpy or in the Python
core.

Cheers,
John
Jun 27 '08 #5

Dan Bishop wrote:
>
Python just uses the atof() function from the underlying C library.
Some of them handle NaN's, and some of them don't.

As a work around, how would I write this in list comprehension form:

newlist=[]
for i in range(len(v[1])):
try:
newlist.append(float(v[1][i]))
except:
newlist.append(-999.99) # or just nan possibly?


--
View this message in context: http://www.nabble.com/numpy%3A-handl...p17870333.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #6
John [H2O] wrote:
Dan Bishop wrote:
>>
Python just uses the atof() function from the underlying C library.
Some of them handle NaN's, and some of them don't.

As a work around, how would I write this in list comprehension form:

newlist=[]
for i in range(len(v[1])):
try:
newlist.append(float(v[1][i]))
except:
newlist.append(-999.99) # or just nan possibly?
from numpy import nan

def nanfloat(x):
if x.lower() == 'nan':
return nan
else:
return float(x)

newlist = [myfloat(x) for x in v[1]]

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave Smithz | last post by:
Hi There, I have taken over someone else's PHP code and am quite new to PHP. I made some changes and have implemented them to a live environment fine so far. However, I now want to setup a...
5
by: Andreas Beyer | last post by:
Hi, How do I find out if NaN, infinity and alike is supported on the current python platform? I could do the following: try: nan = float('NaN') have_nan = True except ValueError: have_nan =...
1
by: Jonathan Fong | last post by:
Hi, I am seeking advice about handling different versions of APIs of office which I am writing a plug-in application for it in C# language. Which is the best way to do? 1. Resolving...
2
by: Jack Russell | last post by:
Some countries use comma instead of point as the decimal separator (and presumably there are other variations) If one has a set of ascii files containing numbers stored in one culture is there...
22
by: Andy McDonagh | last post by:
Dear python experts, I am new to python and this site, so I apologize if this is off topic (i.e. is it a SciPy question?). I will try to demonstrate my problem below:...
1
by: hansman | last post by:
i would like to make a search page in which i can search google, and yahoo. The user may specify what site they with to use by a drop down menu, how can i code the page so that the options in teh...
17
by: Michael Hoffman | last post by:
What's the best way to portably generate binary floating point infinity and NaNs? I only know two solutions: 1. Using the fpconst module proposed in IEEE 754, which I believe shifts bits around....
0
by: Christian Heimes | last post by:
John wrote: I've fixed the issue for Python 2.6 and 3.0 a while ago. Mark and I have spent a lot of time on fixing several edge cases regarding inf, nan and numerical unsound functions in...
1
by: parez | last post by:
HI, Whats the best way of handling a DPI which is different from development machine? In my current case resolution is not an issue as all machines have the same resoluation.. Should i get...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.