7

I have written a function that outputs a double, upto 25 decimal places. I am trying to print it as a formatted output from Raku.

However, the output is incorrect and truncated.

See MWE:

my $var = 0.8144262510988963255087469;
say sprintf("The variable value is: %.25f", $var) 

The above code gives The variable value is: 0.8144262510988963000000000 which is not what is expected.

Also, this seems weird:


my $var = 0.8144262510988963255087469;

say $var.Str.chars; # 29 wrong, expected 27

I tested the same in C:

#include <stdio.h>

int main() {
   double var = 0.8144262510988963255087469;
   printf("The variable value is: %.25lf \n", var);

   return 0;
}

However, it works fine. Given the identical nature of sprintf and printf, I expected this C example to work in Raku too. Seems like %lf is not supported.

So is there a workaround to fix this?

1

2 Answers 2

5

I think this is actually a bug in how Rat literals are created. Or at least as WAT :-).

I actually sort of expect 0.8144262510988963255087469 to either give a compile time warning, or create a Num, as it exceeds the standard precision of a Rat:

raku -e 'say 0.8144262510988963255087469'
0.814426251098896400086204416

Note that these are not the same.

There is fortunately an easy workaround, by creating a FatRat

$ raku -e 'say 0.8144262510988963255087469.FatRat'
0.8144262510988963255087469

FWIW, I think this is worthy of creating an issue

11
  • Can you say a bit more about what it means to "exceed the standard precision of a Rat"? What especially confuses me here is that the precision seems to be present but not printed: say 0.8144262510988963255087469.raku prints <8144262510988963255087469/10000000000000000000000000> Jan 11, 2022 at 12:42
  • @codesections Imo understanding why "output routines like say or put do not try very hard" to display numbers with the precision someone might expect (to (ab)use a suitable phrase from the Numerics page) would best include taking on board related issues in the Rakudo issue queue (and probably the old Raku issue queue too; important unresolved info in old issues has not yet made it to the new queue). Fwiw I'm working on reviewing/reporting related issues as part of another answer to this Q.
    – raiph
    Jan 11, 2022 at 13:28
  • @raiph I have indeed read the Numerics page :) In fact, that page (more specifically, the Degradation to Num section) is what has me so confused. From that page, I would expect that, with my $var = 0.8144262510988963255087469, printing $var would degrade $var to a Num and thus that say $var and say $var.Num` would print the same string. But, in reality, they don't. (Both output strings start with 0.814426251098896, but the Rat ends with 400086204416 while the Num ends with 3 – neither of which is correct.) Jan 11, 2022 at 13:39
  • 1
    Hmmm... I wonder if we couldn't simplify the rendering of Rat / FatRat to basically stringifying the numerator if the denominator is a power of 10? Jan 11, 2022 at 13:50
  • 1
    @codesections Otoh, I never arrived at sufficient clarity to be confident about any particular tweak of what Z wrote in that page. Perhaps this time, as we focus once more on the same set of issues that have been thoroughly explored for decades in the Perl and Raku communities, we can collectively get to some suitable doc tweaks, and perhaps some Raku(do) tweaks? At least, that's what I'm hoping.
    – raiph
    Jan 11, 2022 at 14:39
2

From your question:

I have written a function that outputs a double, upto 25 decimal places.

From google:

Double precision numbers are accurate up to sixteen decimal places

From the raku docs :

When constructing a Rat (i.e. when it is not a result of some mathematical expression), however, a larger denominator can be used

so if you go

my $v = 0.8144262510988963255087469; 
say $v.raku;
#<8144262510988963255087469/10000000000000000000000000>

it works.

However, do a mathematical expression such as

my $b = $a/10000000000000000000000000;

and you get the Rat => Num degradation applied unless you explicitly declare FatRats. I visualise this as the math operation placing the result in a Num register in the CPU.

The docs also mention that .say and .put may be less faithful than .raku, presumably because they use math operations (or coercion) internally.

Sorry to be the bearer of bad news, but 10**25 > 2 **64, but what you report as an issue is correct & (fairly) well documented behaviour given the constraints of double precision IEEE P754.

2
  • Upvoted but/also: ❶ [op means] "Rat => Num degradation applied unless ..." See Introducing $*RAT-OVERFLOW. ❷ ".say and .put may be less faithful than .raku, presumably because they use math operations (or coercion) internally" say uses gist. See what does gist mean?. put uses .Str. This isn't a gist, but is still approximate for Nums. (Rationale is Nums are themselves approximate.) ❸ "correct & (fairly) well documented ... given constraints" I wish it was that simple! :)
    – raiph
    Jan 15, 2022 at 11:21
  • @p6steve Thanks for the info. I was implementing brute-force kendall-tau coefficient in C (to later call from Raku), run here. I tested the same example in R language to see the accuracy of the output from my C. The output in my C example matched up to 22 places with R output, and thought even Raku would display that. As per IEEE P754, kendal_tau_cor output in C should have degraded beyond 16 digits. Jan 16, 2022 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.