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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With