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

make use of scientific notation for BigFloat consistent with other types #29211

Merged
merged 1 commit into from
Dec 3, 2018

Conversation

rfourquet
Copy link
Member

@rfourquet rfourquet commented Sep 16, 2018

This switches to non-scientific notation when the scientific exponent
would be in -4:5, as is the case for e.g. Float64.
For example, string(big(42.0)) is now "42.0".

This is a change I've wanted for quite a while, I don't know if I'm in the minority here. I didn't find a discussion on this topic (except for removing a useless "e+00"), so I'm not sure whether the current printing is just inherited from the choices made in MPFR, or if this was a conscious decision.
I mostly use BigFloat in a range where I find the scientific notation as an obstacle to my human parsing, e.g.

julia> big"42.0"                                                                                                         
4.2e+01

julia> rand(BigFloat, 2, 2)
2×2 Array{BigFloat,2}:
 5.59682e-01  9.24952e-01
 4.77888e-01  4.98624e-02

julia> 2 * rand(BigFloat, 3)
3-element Array{BigFloat,1}:
 1.119363387037806864843608696550048745013519509937459521981246563117370317221463
 1.557756144279027196473368290932951926003919517867431965355848779211876826219583e-01
 1.849904313908958285396788230625007253199545024377884487188520977470270959385811

In particular in the last example, it's easy for me to forget to look at the end to check if there is an exponent.
With this PR, this is turned into

julia> big"42.0"                                                                                                         
42.0

julia> rand(BigFloat, 2, 2)
2×2 Array{BigFloat,2}:
 0.559682  0.924952
 0.477888  0.0498624

julia> 2 * rand(BigFloat, 3)
3-element Array{BigFloat,1}:
 1.119363387037806864843608696550048745013519509937459521981246563117370317221463
 0.1557756144279027196473368290932951926003919517867431965355848779211876826219583
 1.849904313908958285396788230625007253199545024377884487188520977470270959385811

@rfourquet rfourquet added display and printing Aesthetics and correctness of printed representations of objects. bignums BigInt and BigFloat labels Sep 16, 2018
@rfourquet rfourquet force-pushed the rf/bigfloat-print branch 2 times, most recently from f3e0127 to 5543ac0 Compare September 16, 2018 12:34
@StefanKarpinski StefanKarpinski added the minor change Marginal behavior change acceptable for a minor release label Sep 16, 2018
@StefanKarpinski StefanKarpinski added this to the 1.1 milestone Sep 16, 2018
@JeffBezanson
Copy link
Sponsor Member

This seems pretty inefficient --- is there a way to do this via the mpfr_sprintf format string?

@JeffBezanson JeffBezanson added the needs news A NEWS entry is required for this change label Nov 29, 2018
@rfourquet
Copy link
Member Author

This seems pretty inefficient

I just did basic benchmarks to check that, and it's (only) about 35% slower.

is there a way to do this via the mpfr_sprintf format string?

I tried that but didn't find how (IIRC), so ended up with the handmade version.

This switches to non-scientific notation when the scientific exponent
would be in `-4:5`, as is the case for e.g. Float64.
For example, `string(big(42.0))` is now `"42.0"`.
@rfourquet rfourquet removed the needs news A NEWS entry is required for this change label Nov 30, 2018
@JeffBezanson
Copy link
Sponsor Member

Ok. This is a very good change, so I'll merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bignums BigInt and BigFloat display and printing Aesthetics and correctness of printed representations of objects. minor change Marginal behavior change acceptable for a minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants