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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.