Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Exploiting Perls idea of what is a number

by graff (Chancellor)
on Jul 09, 2008 at 03:10 UTC ( [id://696375]=note: print w/replies, xml ) Need Help??


in reply to Exploiting Perls idea of what is a number

The following snippet is almost certainly not what you want, but I find it instructive...
#!/usr/bin/perl use strict; my $numrgx = qr/^-?[\d.]+(?:e[+-]\d{2})?$/; my @try_these = ( "0.2000000000000001", "0.3000000000000001" ); for ( @try_these ) { my $x = my $y = $_; $y .= "1"; my $same = ( $x eq $y ); if ( ! $same and ( $x =~ /^-?\.?\d/ and $x =~ /$numrgx/ ) and ( $y =~ /^-?\.?\d/ and $y =~ /$numrgx/ )) { $same = ( $x == $y ); } my $cmp = ( $same ) ? "is the same as" : "differs from"; print "$x $cmp $y\n"; }
For me (macosx 10.4 on intel core 2 duo, perl 5.8.6), the output is:
0.2000000000000001 differs from 0.20000000000000011 0.3000000000000001 is the same as 0.30000000000000011
Have fun...

BTW, the reason for the funny looking condition -- checking matches for both  /^-?\.?\d/ and for the more elaborate regex -- was that it seemed like an easy way to keep $numrgx relatively clear and simple, while still making sure that the string contains at least one digit (which $numrgx by itself does not guarantee).

(updated the initial match condition that checks for at least one digit, so that it would only accept "\d", "-\d", ".\d" or "-.\d" at the beginning of the string; of course, $numrgx is still faulty, since it allows multiple periods)

Another update -- I knew the regex approach above was silly, and I was curious to have a version that would make it easier to test for more boundary cases (where floating point limitations blur the "same/different" distinction), so here's a version with a better regex -- still not perfect, I suspect, but more fun because of the @ARGV options:

#!/usr/bin/perl use strict; my $numrgx = qr/^-?(?:\d+\.?\d*|\d*\.\d+)(?:e[+-]?\d{1,2})?$/; my @try_these = ( @ARGV == 2 ) ? ( $ARGV[0] ) : ( "0.2000000000000001", "0.3000000000000001" ); for ( @try_these ) { my $x = $_; my $y = ( @ARGV == 2 ) ? $ARGV[1] : $_ . "1"; my $same = ( $x eq $y ); if ( ! $same and ( $x =~ /$numrgx/ ) and ( $y =~ /$numrgx/ )) { $same = ( $x == $y ); } my $cmp = ( $same ) ? "is the same as" : "differs from"; print "$x $cmp $y\n"; }
With that, you can put a pair like "0.123e4 1.230e3" on the command line, and find out that these are the same; and if you do a pair like "0.123e45 1.230e44", these are different. (final update/fix was to add "?" after the first period in $numrgx)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://696375]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found