From:

Date:

May 31, 2001 07:10Subject:

Re: test for real numberMessage ID:

m1ae3tboeu.fsf@halfdome.holdit.comThe most comprehensive test for a real number is to let Perl do it itself: sub is_number { my $bad = 0; local $SIG{__WARN__} = sub { $bad++ }; local $^W = 1; my $guess = shift; $guess += 0; return not $bad; } If adding 0 didn't trigger the numeric warning, then it's a good number! For integers, you can narrow it down: sub is_integer { my $bad = 0; local $SIG{__WARN__} = sub { $bad++ }; local $^W = 1; my $guess = shift; return $guess == int($guess) and not $bad; } -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!Thread Previous | Thread Next

- test for real number by mikemckee
- Re: test for real number by kimball
- Re: test for real number by merlyn
- Re: test for real number by Morten Sickel
**Re: test for real number**by merlyn- Re: test for real number by Paul
- Re: test for real number by merlyn
- advanced: closures (was Re: test for real number) by Jeff Pinyan
- Re: advanced: closures (was Re: test for real number) by merlyn
- Re: advanced: closures (was Re: test for real number) by Jeff Pinyan
- Re: advanced: closures (was Re: test for real number) by merlyn
- Re: advanced: closures (was Re: test for real number) by Peter Scott
- Re: advanced: closures (was Re: test for real number) by merlyn
- Re: test for real number by Paul
- Re: test for real number by Piers Cawley
- Re: test for real number by Paul
- Re: test for real number by Paul
- Re: test for real number by Carl Rogers
- Re: test for real number by Jeff Pinyan
- Re: test for real number by Paul
- Re: test for real number by Walt Mankowski

nntp.perl.org: Perl Programming lists via nntp and http.

Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About