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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.