Skip to content

Commit

Permalink
make use of scientific notation for BigFloat consistent with other ty…
Browse files Browse the repository at this point in the history
…pes (#29211)

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"`.
  • Loading branch information
rfourquet authored and JeffBezanson committed Dec 3, 2018
1 parent 6b04291 commit 7ea4542
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Standard library changes
returns a `BigFloat` with precision equal to `precision(BigFloat)` ([#29127]). The optional
`precision` argument to override the global setting is now a keyword instead of positional
argument ([#29157]).
* The use of scientific notation when printing `BigFloat` values is now consistent with other floating point
types ([#29211]).
* `Regex` and `TimeZone` now behave like scalars when used in broadcasting ([#29913], [#30159]).
* `Char` now behaves like a read-only 0-dimensional array ([#29819]).
* `parse` now allows strings representing integer 0 and 1 for type `Bool` ([#29980]).
Expand Down
2 changes: 1 addition & 1 deletion base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ julia> big"123_456"
123456
julia> big"7891.5"
7.8915e+03
7891.5
```
"""
macro big_str(s)
Expand Down
2 changes: 1 addition & 1 deletion base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ julia> sqrt(big(81))
9.0
julia> sqrt(big(-81))
ERROR: DomainError with -8.1e+01:
ERROR: DomainError with -81.0:
NaN result for non-NaN input.
Stacktrace:
[1] sqrt(::BigFloat) at ./mpfr.jl:501
Expand Down
16 changes: 14 additions & 2 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,20 @@ function _prettify_bigfloat(s::String)::String
if endswith(mantissa, '.')
mantissa = string(mantissa, '0')
end
if exponent == "+00"
mantissa
expo = parse(Int, exponent)
if -5 < expo < 6
expo == 0 && return mantissa
int, frac = split(mantissa, '.')
if expo > 0
expo < length(frac) ?
string(int, frac[1:expo], '.', frac[expo+1:end]) :
string(int, frac, '0'^(expo-length(frac)), '.', '0')
else
neg = startswith(int, '-')
neg == true && (int = lstrip(int, '-'))
@assert length(int) == 1
string(neg ? '-' : "", '0', '.', '0'^(-expo-1), int, frac)
end
else
string(mantissa, 'e', exponent)
end
Expand Down
2 changes: 1 addition & 1 deletion base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Multiply `x` and `y`, giving the result as a larger type.
# Examples
```jldoctest
julia> widemul(Float32(3.), 4.)
1.2e+01
12.0
```
"""
widemul(x::Number, y::Number) = widen(x)*widen(y)
Expand Down
4 changes: 2 additions & 2 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ julia> repr(zeros(3))
"[0.0, 0.0, 0.0]"
julia> repr(big(1/3))
"3.33333333333333314829616256247390992939472198486328125e-01"
"0.333333333333333314829616256247390992939472198486328125"
julia> repr(big(1/3), context=:compact => true)
"3.33333e-01"
"0.333333"
```
"""
Expand Down
16 changes: 13 additions & 3 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -653,15 +653,15 @@ end
@test string(nextfloat(BigFloat(1))) == str
end
setprecision(21) do
@test string(parse(BigFloat, "0.1")) == "1.0000002e-01"
@test string(parse(BigFloat, "0.1")) == "0.10000002"
@test string(parse(BigFloat, "-9.9")) == "-9.9000015"
end
setprecision(40) do
@test string(parse(BigFloat, "0.1")) == "1.0000000000002e-01"
@test string(parse(BigFloat, "0.1")) == "0.10000000000002"
@test string(parse(BigFloat, "-9.9")) == "-9.8999999999942"
end
setprecision(123) do
@test string(parse(BigFloat, "0.1")) == "9.99999999999999999999999999999999999953e-02"
@test string(parse(BigFloat, "0.1")) == "0.0999999999999999999999999999999999999953"
@test string(parse(BigFloat, "-9.9")) == "-9.8999999999999999999999999999999999997"
end
end
Expand Down Expand Up @@ -941,6 +941,16 @@ end
test_show_bigfloat(big"-1.23456789", contains_e=false, starts="-1.23")
test_show_bigfloat(big"2.3457645687563543266576889678956787e10000", starts="2.345", ends="e+10000")
test_show_bigfloat(big"-2.3457645687563543266576889678956787e-10000", starts="-2.345", ends="e-10000")
test_show_bigfloat(big"42.0", contains_e=false, starts="42.0")
test_show_bigfloat(big"420.0", contains_e=false, starts="420.0") # '0's have to be added on the right before point
test_show_bigfloat(big"-420.0", contains_e=false, starts="-420.0")
test_show_bigfloat(big"420000.0", contains_e=false, starts="420000.0")
test_show_bigfloat(big"654321.0", contains_e=false, starts="654321.0")
test_show_bigfloat(big"-654321.0", contains_e=false, starts="-654321.0")
test_show_bigfloat(big"6543210.0", contains_e=true, starts="6.5", ends="e+06")
test_show_bigfloat(big"0.000123", contains_e=false, starts="0.000123")
test_show_bigfloat(big"-0.000123", contains_e=false, starts="-0.000123")
test_show_bigfloat(big"0.00001234", contains_e=true, starts="1.23", ends="e-05")

for to_string in [string,
x->sprint(show, x),
Expand Down

0 comments on commit 7ea4542

Please sign in to comment.