Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl6 precision base4 conversion

Tags:

raku

Perl 6 is losing precision when converting to/from base4. How to retain precision?

'0.2322130120323232322110'.parse-base(4)
--> perl6 output :         0.728295262649453
--> high precission value: 0.728295262649453434278257191181182861328125

The problem is, when converting 0.728295262649453 to base(4), output is not the original number.

0.72829526264945.base(4)
--> output:   0.232213012032323232210333
--> original: 0.2322130120323232322110

How to get same values after to/from conversion?

like image 578
rx57 Avatar asked Mar 09 '23 04:03

rx57


1 Answers

The problem is probably in the way you created your "perl6 output":

say "0.2322130120323232322110".parse-base(4)    # 0.72829526264945

This is because say calls the .gist method on whatever it is given. Or you tried to stringify it (which calls .Str, which gives the same result as .gist). If you would call the .perl method on the result:

say "0.2322130120323232322110".parse-base(4).perl

you do get the expected 0.728295262649453434278257191181182861328125. The .perl method returns a string that you could EVAL to get the originally given value.

In any case, if you do:

say "0.2322130120323232322110".parse-base(4).base(4)

you will see that you do get back the original value 0.2322130120323232322110. I guess this is just a case of just doing it rather than saying it. :-)

One could argue that .Str on a Rat should use .perl instead of .gist. Perhaps that should be a point of attention: it would probably have prevented you from needing to ask this question.

like image 76
Elizabeth Mattijsen Avatar answered Mar 30 '23 08:03

Elizabeth Mattijsen