New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sprintf %f bogus rounding #5519
Comments
From zefram@fysh.org
sprintf %f is not showing the true value of this Num, which it should. -zefram |
From @pmichaudOn Tue, Aug 02, 2016 at 09:56:38AM -0700, Zefram wrote:
Nums hold only 15-17 digits of precision, so sprintf("%f") In particular, the true value is *not* always available, as illustrated by: > (1180591620717411303424.0e0).Int Pm |
The RT System itself - Status changed from 'new' to 'open' |
From zefram@fysh.orgPatrick R. Michaud via RT wrote:
No, it does not have this correct. The value represented in the Num is
By "true value" I meant the value represented in floating point. It's sounding, in this and [perl #128817], rather as though you're -zefram |
From @pmichaudOn Tue, Aug 02, 2016 at 07:46:20PM +0100, Zefram wrote:
My apologies, I did not catch this meaning of "true value" from I agree that having sprintf("%f") automatically truncate with zeros Pm |
From zefram@fysh.orgPatrick R. Michaud wrote:
doc/Type/Str.pod (which I checked before reporting the bug) says it's # f,F A double argument representing a (finite) floating-point number This is slightly less clear than I expected: there's a tiny bit of room -zefram |
From zefram@fysh.orgA related case, now that I've actually looked at the implementation:
The Num value in this case is exactly 2251799813685577/2251799813685248, What's going on is that the implementation is first converting to -zefram |
From zefram@fysh.orgAttached is a straightforward implementation of the floating-point I needed some supporting floating-point utilities, for which I translated An observation: testing floating-point code is substantially impeded by A consequent suggestion: rather than working on decimal floating-point -zefram |
From zefram@fysh.org# writable scalar references my sub ref_to(Mu $a is rw) { my @s; @s[0] := $a; @s } # floating-point utilities (imitating most of Perl 5 Data::Float) my @powtwo = (2e0,); sub mult_pow2(Num:D $value is copy, Int:D $exp is copy) { my $min_finite_exp; my @directions = ( while !@directions[0]<done> || !@directions[1]<done> { for @directions -> $direction { sub pow2(Int:D $exp where $exp >= $min_finite_exp && $exp <= $max_finite_exp) { my $significand_bits; my $max_finite = $max_finite_pow2 - my $max_integer = pow2($significand_bits + 1); my $have_subnormal; my $min_normal_exp = $have_subnormal ?? $min_finite_exp + $significand_bits !! my $have_signed_zero = /^\-/.ACCEPTS((-0e0).Str).Bool; enum FloatClass <Normal Subnormal Zero Infinite Nan>; sub float_class(Num:D $val is copy) { sub float_is_normal(Num:D $val) { float_class($val) == Normal } sub float_is_subnormal(Num:D $val) { float_class($val) == Subnormal } sub float_is_infinite(Num:D $val) { $val == Inf || $val == -Inf } sub float_is_nzfinite(Num:D $val) { sub float_is_zero(Num:D $val) { $val == 0e0 } sub float_is_finite(Num:D $val) { $val == $val && !float_is_infinite($val) } sub float_is_nan(Num:D $val) { $val != $val } sub signbit(Num:D $val) { enum FloatSign <Positive Negative>; sub float_sign(Num:D $val where !float_is_nan($val)) { sub float_parts(Num:D $val is copy where float_is_nzfinite($val)) { sub copysign(Num:D $val, Num:D $signfrom) { sub nextup(Num:D $val) { sub nextdown(Num:D $val) { -nextup(-$val) } sub nextafter(Num:D $val, Num:D $dir) { # floating-point output my sub float_bin_scale(Num:D $val where float_is_finite($val)) { my sub float_dec_scale(Num:D $val where float_is_finite($val)) { my sub floor_log10(Int:D $val is copy where $val >= 1) { my sub round2(Int:D $val where $val >= 0, Int:D $losebits) { my sub round10(Int:D $val where $val >= 0, Int:D $losedigits) { sub float_sprintf( # main program for test my $fmt = @*ARGS.shift; for @*ARGS { |
While I know this is the old-issues-tracker, I've decided to add a comment here rather than open an issue in the new issues tracker. I'll do that as a later step. For now I wanted to paste this clean version of code from the previous comment above that was generated by p6rt. It contains invisible bogus characters that I've stripped so that the code compiles.
|
Migrated from rt.perl.org#128818 (status was 'open')
Searchable as RT128818$
The text was updated successfully, but these errors were encountered: