Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,

I ran the following script with raku:
my $d_1 = 0.1e0; # double 0.1 ( == 3602879701896397/3602879701896396 +8 ) my $r_1 = 0.1; # rational 1/10 my $d_2 = 0.10000000000000001e0;# double, same value as $d_1 my $r_2 = 0.10000000000000001; # rational 10000000000000001/100000000 +000000000 # check that $d_1 == 3602879701896397/36028797018963968 say "not ok 0" if $d_1 != 3602879701896397/36028797018963968; # Check that $d_1 != 1 / 10; say "not ok 1" if $d_1 == 1 / 10; # Check that $d_1 and $d_2 are assigned to exactly the same value: say "not ok 2" if $d_1 != $d_2; # Check that $r_ and $r_2 are assigned different values: say "not ok 3" if $r_1 == $r_2; # Since $r_1 and $r_2 are unequal, $d_1 should not be equal to both $r +_1 && $r_2. # We check this, interchanging lhs and rhs operands in case that makes + a # difference: say "not ok 4" if ($d_1 == $r_1 && $d_1 == $r_2); say "not ok 5" if ($r_1 == $d_1 && $r_2 == $d_1); # Similarly $d_2 should not be equal to both $r_1 and $r_2: say "not ok 6" if ($d_2 == $r_1 && $d_2 == $r_2); say "not ok 7" if ($r_1 == $d_2 && $r_2 == $d_2); say Rat($d_1); say Rat($d_2);
I was surprised that it output:
not ok 1 not ok 4 not ok 5 not ok 6 not ok 7 0.1 0.1
However, I think I understand what's happening.
I had thought that when raku compared a double with a rational, it would convert the double to its exact rational value, and then compare the 2 rationals.
Instead it apparently converts the rational to a double (rounding if necessary), and then compares the 2 doubles.

I can see some sense in performing the comparison in the way they've chosen:
1) It's probably much cheaper to convert a rational to a double than vice-versa;
2) If the rounded rational is equivalent to the double then, under certain circumstances, you might be happy enough to think of the 2 values as being equivalent.

Doing it that way might be a useful comparison to be making, but I don't think it's a comparison that the numeric comparison operators (==, <, >, etc.) should be performing.

Anyway - the first of my questions (and there probably will be more):
Is there a way to get raku to explicitly display the fact that the (double precision) values held by $d_1 and $d_2 are exactly equivalent to the rational value 3602879701896397/36028797018963968 ?

Cheers,
Rob

In reply to [Raku] Arcane treatment of doubles and rationals by syphilis

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 drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found