Skip Menu |
Report information
Id: 128828
Status: new
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: zefram [at]

Severity: (no value)
Tag: Bug
Platform: (no value)
Patch Status: (no value)
VM: (no value)

From: Zefram <zefram [...]>
Subject: [BUG] Num.FatRat coercion is inexact
To: rakudobug [...]
Date: Wed, 3 Aug 2016 11:33:42 +0100
Download (untitled) / with headers
text/plain 1.3k
Show quoted text
> my $a = 2476979795053773e0 * (2e0**-51)
1.1 Show quoted text
> ($a * (2e0**51)).Int
2476979795053773 Show quoted text
> ($a * (2e0**51)) % 1e0
0 Show quoted text
> $a.FatRat.perl, 10) The Num->FatRat conversion is being performed inexactly. Every finite Num value is in fact rational, and therefore exactly representable as a FatRat, so I would expect the coercion to preserve the value exactly. It should come out as, 2251799813685248). Not only is 11/10 not the exact value of this Num, it's not the exact value of any Num, because the floating point format can only represent values whose denominators are powers of two. The same considerations would not in general apply to the .Rat coercion. Because the Rat type is intended to have a limited range of denominators, it cannot exactly represent all Num values. However, I believe the denominator in this particular case, 2**51, is in the intended range. So in this case $a.Rat also ought to be exact, but actually it also isn't. Note that in the above there is no rounding in the decimal->float conversion: the large integer value is less than 2**52, and so fits into a float with a bit to spare. There is also no rounding in the power-of-two or in the multiplication, which just changes the exponent of the float. So the Num $a exactly represents the value that is suggested by the way it was computed. The .Int and %1 operations illustrate the exact value. -zefram

This service is sponsored and maintained by Best Practical Solutions and runs on infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at