Skip to content
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

Num.perl doesn't round-trip numeric value #5518

Closed
p6rt opened this issue Aug 2, 2016 · 8 comments
Closed

Num.perl doesn't round-trip numeric value #5518

p6rt opened this issue Aug 2, 2016 · 8 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Aug 2, 2016

Migrated from rt.perl.org#128817 (status was 'resolved')

Searchable as RT128817$

@p6rt
Copy link
Author

p6rt commented Aug 2, 2016

From zefram@fysh.org

(1180591620717411303424.0e0).Int
1180591620717411303424
(1180591620717411303424.0e0).perl.EVAL.Int
1180591620717409992704

The .perl.EVAL process ought to yield the same value we started with.
It's coming back as a different Num value.

-zefram

@p6rt
Copy link
Author

p6rt commented Aug 2, 2016

From @pmichaud

On Tue Aug 02 09​:51​:31 2016, zefram@​fysh.org wrote​:

(1180591620717411303424.0e0).Int
1180591620717411303424
(1180591620717411303424.0e0).perl.EVAL.Int
1180591620717409992704

The .perl.EVAL process ought to yield the same value we started with.
It's coming back as a different Num value.

Double-precision floating point (which is essentially what "e0" does) generally has only 15-17 digits of decimal precision. I don't know that we should expect .perl or any other operation on Num values to be preserving more precision than that.

  > (1180591620717411303424.0e0+0).perl
  1.18059162071741e+21

In particular, note that the following produce results that are mathematically incorrect but are still within the expected range of behavior for floating-point (Num) values​:

  > (1180591620717411303424.0e0 + 1.0 ).Int
  1180591620717411303424
  > (1180591620717411303424.0e0 + 10 ).Int
  1180591620717411303424

Pm

@p6rt
Copy link
Author

p6rt commented Aug 2, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Aug 2, 2016

From zefram@fysh.org

Patrick R. Michaud via RT wrote​:

I don't know that we should expect .perl or any other operation on Num
values to be preserving more precision than that.

I'd expect .perl to preserve whatever precision is there. Accurately
representing the value, in a form understandable by .EVAL, is its
raison d'etre.

The value I'm using in this example is 2**70, a very round number
that is easily represented in floating point. If the lengthy decimal
representation is putting you off, consider

(2e0**70).perl.EVAL == 2e0**70
False

(1180591620717411303424.0e0 + 1.0 ).Int
1180591620717411303424
(1180591620717411303424.0e0 + 10 ).Int
1180591620717411303424

These are as expected​: floating point rounding applies, and the results
obtained are the closest representable values to the mathematically
correct result. This is a red herring. My example doesn't invoke
any operation requiring rounding. The initial decimal literal has the
exact representable value, which gets represented correctly as a Num.
I don't then perform any arithmetic on it.

-zefram

@p6rt
Copy link
Author

p6rt commented Aug 2, 2016

From @pmichaud

On Tue, Aug 02, 2016 at 07​:32​:56PM +0100, Zefram wrote​:

Patrick R. Michaud via RT wrote​:

I don't know that we should expect .perl or any other operation on Num
values to be preserving more precision than that.

I'd expect .perl to preserve whatever precision is there. Accurately
representing the value, in a form understandable by .EVAL, is its
raison d'etre.
[...]

Okay. I'm agreeable to say that .perl (and perhaps sprintf where
appropriate) should add two more digits of precision to the strings
they produce, to preserve the round-trippiness of the values being
represented.

(1180591620717411303424e0).perl
1.18059162071741e+21
(1.18059162071741e+21).Int # not enough precision
1180591620717409992704
(1.180591620717411e+21).Int # still not enough
1180591620717411041280
(1.1805916207174113e+21).Int # okay, this works
1180591620717411303424

Pm

@p6rt
Copy link
Author

p6rt commented Aug 19, 2016

From zefram@fysh.org

Additional​: the same problem arises with Complex.perl, where the real
or imaginary parts suffer this problem as Nums. Fixing this for Num
won't automatically fix it for Complex, because Complex.perl doesn't
invoke Num.perl.

-zefram

@p6rt
Copy link
Author

p6rt commented Apr 13, 2018

From @zoffixznet

On Tue, 02 Aug 2016 09​:51​:31 -0700, zefram@​fysh.org wrote​:

(1180591620717411303424.0e0).Int
1180591620717411303424
(1180591620717411303424.0e0).perl.EVAL.Int
1180591620717409992704

The .perl.EVAL process ought to yield the same value we started with.
It's coming back as a different Num value.

-zefram

Thank you for the report. This is now fixed.

Fix​: MoarVM/MoarVM@067c0594103a025
  MoarVM/MoarVM@8841c4241b4faa8
  MoarVM/MoarVM@af2eb8a7f7d4344
  MoarVM/MoarVM@4d3fc2818d0032b
  rakudo/rakudo@8422d7b4e23678b
  rakudo/rakudo@a2a2a745c4242d1
  rakudo/rakudo@430266629f2e2a0
Test​: Raku/roast@dcdbcb31b43a0f6ec

@p6rt
Copy link
Author

p6rt commented Apr 13, 2018

@zoffixznet - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Apr 13, 2018
@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant