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

Ryu: add support for maximum significant digits #33520

Merged
merged 8 commits into from
Oct 17, 2019
Merged

Conversation

simonbyrne
Copy link
Contributor

This moves the compact truncation to the main reduction stage, which avoids problems with double rounding.

Fixes #33463

test/ryu.jl Outdated Show resolved Hide resolved
Copy link
Member

@quinnj quinnj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for doing this!

@simonbyrne
Copy link
Contributor Author

simonbyrne commented Oct 14, 2019

The only remaining question is what to do about Float16: the easiest thing I can think of is to change it so that it switches to exponent notation for 1e3 or greater (eps(Float64(1000)) == 0.5, eps(Float16(2048)) == 2). This would be a change from earlier versions.

@quinnj
Copy link
Member

quinnj commented Oct 14, 2019

I think that's a good idea; I feel like the previous rules were weirdly complex/inconsistent.

@simonbyrne
Copy link
Contributor Author

Okay, this now gives

julia> Float16(999)
Float16(999.0)

julia> Float16(1000)
Float16(1.0e3)

@simonbyrne
Copy link
Contributor Author

simonbyrne commented Oct 16, 2019

There are a few weird edge cases where we don't actually print the closest value in order to get a trailing 0 digit:

julia> print(nextfloat(0.0,1))
5.0e-324
julia> @printf "%.1e" nextfloat(0.0,1)
4.9e-324
julia> print(nextfloat(0.0,2))
1.0e-323
julia> @printf "%.1e" nextfloat(0.0,2)
9.9e-324

and for Float32:

julia> print(nextfloat(0f0,1))
1.0f-45
julia> @printf "%.1e" nextfloat(0f0,1)
1.4e-45
julia> print(nextfloat(0f0,2))
3.0f-45
julia> @printf "%.1e" nextfloat(0f0,2)
2.8e-45
julia> print(nextfloat(0f0,3))
4.0f-45
julia> @printf "%.1e" nextfloat(0f0,3)
4.2e-45
julia> print(nextfloat(0f0,4))
6.0f-45
julia> @printf "%.1e" nextfloat(0f0,4)
5.6e-45

The options:

  1. leave as is
  2. print the extra digit correctly
  3. print only one digit and no decimal, e.g. 5e-324 (since these all have exponents, there is no risk of them being parsed as integers).
    • this appears to be what Python does

test/ryu.jl Outdated Show resolved Hide resolved
@simonbyrne
Copy link
Contributor Author

simonbyrne commented Oct 16, 2019

Alright, I've gone for option 3 above, as I think that is the most consistent. Since this and the Float16 printing are changes from earlier versions I'll leave it for triage to decide.

Note that this will change non-subnormal values as well, e.g.

julia> 1e-30
1e-30

julia> 1e30
1e30

@simonbyrne simonbyrne added status:triage This should be discussed on a triage call domain:display and printing Aesthetics and correctness of printed representations of objects. labels Oct 16, 2019
@simonbyrne simonbyrne force-pushed the sb/ryu-split branch 3 times, most recently from 5aa3fde to 7901c74 Compare October 17, 2019 05:50
@simonbyrne simonbyrne force-pushed the sb/ryu-split branch 2 times, most recently from 1bae680 to 06079a6 Compare October 17, 2019 17:56
@simonbyrne
Copy link
Contributor Author

Okay, it has become clear that would be a bigger change than I thought. I'll save that for a new issue.

@simonbyrne simonbyrne removed the status:triage This should be discussed on a triage call label Oct 17, 2019
@simonbyrne simonbyrne merged commit 1d8ec2f into master Oct 17, 2019
@simonbyrne simonbyrne deleted the sb/ryu-split branch October 17, 2019 22:29
@StefanKarpinski
Copy link
Sponsor Member

That decision seems fine to me 👍

@quinnj
Copy link
Member

quinnj commented Oct 18, 2019

Thanks for doing this @simonbyrne; it makes me feel much better knowing someone else has done a deep dive into this code. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Floating point printing bug whack-a-mole
4 participants