Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: [Raku] Arcane treatment of doubles and rationals

by holli (Abbot)
on Apr 04, 2021 at 13:16 UTC ( #11130803=note: print w/replies, xml ) Need Help??

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

$ raku -e "say (838861/8388608).raku" 0.10000002384185791015625 $ raku -e "dd 838861/8388608" 0.10000002384185791015625 $ raku -e "say (838861/8388608).gist" 0.10000002 $ raku -e "say 838861/8388608" 0.10000002

If you use say It will call gist on the object to say, trying to provide something human (easily) readable. Use dd instead or call the raku method.


You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^3: [Raku] Arcane treatment of doubles and rationals
by syphilis (Bishop) on Apr 05, 2021 at 05:18 UTC
    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.

      $ perl -le 'printf "%.800g\n", 0.1'

      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 ( )

      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.)


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130803]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-09-17 03:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found