in reply to Re^2: [Raku] Arcane treatment of doubles and rationals
in thread [Raku] Arcane treatment of doubles and rationals

Thanks - the raku and gist methods were what I needed to locate and comprehend.
$ raku -e 'my $r = 3602879701896397/36028797018963968; say $r.raku; sa +y $r.raku == $r;' 0.1000000000000000055511151231257827021181583404541015625 True

This is much more like what I expected of the raku language.
I'm surprised that the gist method sacrifices accuracy so readily. Even 1/128 does not render the exact value, providing 0.007813 instead of 0.0078125.
But that's a matter of choice - I now see nothing buggy happening there.

As regards converting a double to its exact rational representation, this is quite simple in perl:
$ perl -le 'printf "%.800g\n", 0.1' 0.1000000000000000055511151231257827021181583404541015625 $
Is the same thing also as straightforward in raku ?
I tried:
$ raku -e 'printf "%.800g\n", 0.1e0' NaN $
I've read documentation stating that raku's printf() expects that the last argument must be a string.

Cheers,
Rob

Replies are listed 'Best First'.
Re^4: [Raku] Arcane treatment of doubles and rationals
by syphilis (Archbishop) on Apr 06, 2021 at 12:03 UTC
    $ perl -le 'printf "%.800g\n", 0.1'
    0.1000000000000000055511151231257827021181583404541015625


    You can do essentially the same in python3:
    $ python3 -c 'print ("%.800g" % 0.1e0)' 0.1000000000000000055511151231257827021181583404541015625
    It just remains to be seen if and how the same can be achieved in raku.
    I've now posted about this issue to the perl6-users list ( https://www.nntp.perl.org/group/perl.perl6.users/2021/04/msg9860.html )

    Of course, we don't have to ask for 800 digits for the particular value of 0.1, as it only requires 55 significant digits. But there are some double precision values that do need more.
    For example 4.4501477170144023e-308 requires 767 significant digits. I don't know of any value that requires more than 767 digits ... I'm confident (for some dubious definition of "confident") there aren't any.
    The value of 4.4501477170144023e-308 can be assigned with:
    C:\> perl -e "for(1022 .. 1074) { $x+= 2 ** -$_ }; printf '%.16e', $x"
    I've also found that when python3 and the mpfr library compare rationals and doubles, they do so by converting the double to its exact rational value, and then compare the 2 rationals.
    I haven't found any implementation (apart from raku) that converts the rational to its nearest double value, and then compares the doubles.
    (I've also mentioned this in the same perl6-users thread.)

    Cheers,
    Rob