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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)

In reply to Re: Exploiting Perls idea of what is a number by graff
in thread Exploiting Perls idea of what is a number by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found