From Chris Barker
Jean-Michel Fauth wrote:
> You are probably right, I do not know. It is working
> nicely on my win98 platform.
I'm kind of a platform independent fanatic...
Do you succeed to write KDE/GNOME independent apps? ...
···
---------
> You may not believe this:
> Again on my win platform, you method returns False for 1 and -1 !!!
which method is that? I did find that:
float("NaN") == 1.0
returns true, oddly enough, so you can't explicitly test for NaN. Both
the enclosed methods seem to work for me.
Oops I made a mistake. I was not testing NaN, but I have to many
IsFloat's in my test module.
I prefer to recognize my error instead of having found an error in Python!
-----
> Anyway, I tested the fpconst mudule. It is working fine here.
what's the fpconst module?
Just the module you proposed in a previous post!
http://software.biostat.washington.edu/statsoft/snake/fpconst
#-*- coding: iso-8859-1 -*-
#-------------------------------------------------------
# testIsFloat2.py
# Jean-Michel Fauth
# 15 January 2004
#-------------------------------------------------------
import fpconst
#-------------------------------------------------------
def IsFloat_cb(s):
#I define these constants *in* the def for clarity
PlusInf = 1e1000
MinusInf = -1e1000
NotANumber = PlusInf / MinusInf
Invalid = [PlusInf, MinusInf]
try:
x = float(s)
if x in Invalid:
return False
else:
return True
except ValueError:
return False
#-------------------------------------------------------
#uses the fpconst module
def IsFloat_fpconst(s):
try:
x = float(s)
return fpconst.isFinite(x)
except:
return False
#-------------------------------------------------------
if __name__ == '__main__':
nnn = ['0', '1', '-1', "23.43", "-234e43", "23e43543", "-342-234321", "-34e43212344", 'abc', '1.0e-3333']
print '--' * 10
for x in nnn:
if IsFloat_cb(x):
print x, "is a Float"
else:
print x, "is not a Float"
print '--' * 10
for x in nnn:
if IsFloat_fpconst(x):
print x, "is a Float"
else:
print x, "is not a Float"
#eof----------------------------------------------------------------
Jean-Michel Fauth, Switzerland