472,354 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 2507
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.