Skip to content
Permalink
Browse files

make use of scientific notation for BigFloat consistent with other types

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 committed Sep 16, 2018
1 parent 26b6a58 commit 5543ac01a1c3c5bfbb1f75c811f48aaeae7e6ce1
Showing with 32 additions and 10 deletions.
  1. +1 −1 base/int.jl
  2. +1 −1 base/math.jl
  3. +14 −2 base/mpfr.jl
  4. +1 −1 base/number.jl
  5. +2 −2 base/strings/io.jl
  6. +13 −3 test/mpfr.jl
@@ -589,7 +589,7 @@ julia> big"123_456"
123456
julia> big"7891.5"
7.8915e+03
7891.5
```
"""
macro big_str(s)
@@ -492,7 +492,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
@@ -942,8 +942,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
@@ -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)
@@ -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"
```
"""
@@ -631,15 +631,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
@@ -919,6 +919,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),

0 comments on commit 5543ac0

Please sign in to comment.
You can’t perform that action at this time.