validation for float input

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

Jean-Michel Fauth wrote:

From Chris Barker

I'm kind of a platform independent fanatic...

Do you succeed to write KDE/GNOME independent apps? ...

I run KDE< I write using wxPythonGTK, so my apps look more GNOME-like, I suppose, but there is no problem running GTK apps under KDE and KDE apps under GNOME. They just look a little different and "feel" much the same. Anyone that is used to *nix is used to apps all looking different...

what's the fpconst module?

Just the module you proposed in a previous post!

Oh. well I had altered a previous posters code, and hadn't come up with that name myself, so I forgot the name. oops. The only porblem with that version is that it does not test for NaNs, because

float("NaN") == x

returns true for any number, x, as far as I can tell. At least on my machine. That lpooks to me like a non IEE754 compliant response, but I'm no expert. Anyway, as long as your users don't type "NaN", it will work! If you want something more robust, there is a PeP with more robust code, that checks for the actaul bits that define Inf, -Inf and NaN.

I think we've beaten this one into the ground...

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov