Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Detect if a scalar is NaN

by User36 (Initiate)
on Jun 21, 2005 at 16:08 UTC ( #468728=perlquestion: print w/replies, xml ) Need Help??

User36 has asked for the wisdom of the Perl Monks concerning the following question:

Hey, Im at work working on a script and I run into nan is there a fuction that checks to make sure my number is a valid number so i can do something like: unless (NaNfuction($mynumber)) { #do stuff } if not is there some way other way to check? ------------ haha wow sorry excuse me. I added 350 lines to an already buggy script so im doing 1921092 things at once and I wasnt paying much attention to my nan issue

Edited by Arunbear: Changed title from 'nan', as per Monastery guidelines

Replies are listed 'Best First'.
Re: Detect if a scalar is NaN
by ikegami (Pope) on Jun 21, 2005 at 16:21 UTC
Re: Detect if a scalar is NaN
by dorward (Curate) on Jun 21, 2005 at 16:23 UTC
Re: Detect if a scalar is NaN
by davidj (Priest) on Jun 21, 2005 at 16:24 UTC
    On page 44 of the Perl Cookbook there is an excellent discussion on how to test whether or not a string is a number. If you do not already have the book, I highly recommend you get it.
    davidj
      Yea I have it. It is a great resource thanks for the help.
Re: Detect if a scalar is NaN
by djohnston (Monk) on Jun 21, 2005 at 17:34 UTC
    I've peeked at the source for Scalar::Util (versions 1.13 and 1.15), and apparently its looks_like_number function will detect NaN, Inf and Infinity depending upon your version of Perl. Here are some related nodes which contain some information that may be of some help to you:

    Detecting if a scalar has a number or string
    Extended IEEE 754 parsing

Re: Detect if a scalar is NaN
by ikegami (Pope) on Jun 21, 2005 at 16:41 UTC
    I got 3 replies and in the tuts they send me too it explicitly says "will not catch NaN" hehe,

    That's good, isn't it? Do you really want your function to consider (the IEEE value) NaN a number? True, it won't report infinity or -infinity as a number. Is that a problem?

      ......wow excuse me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://468728]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2019-05-20 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (125 votes). Check out past polls.

    Notices?