Skip to content

Commit

Permalink
tweak printing of underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Oct 17, 2019
1 parent 06079a6 commit 1bae680
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ For example,
# Examples
```jldoctest
julia> reinterpret(Float32, UInt32(7))
1.0f-44
1f-44
julia> reinterpret(Float32, UInt32[1 2 3 4 5])
1×5 reinterpret(Float32, ::Array{UInt32,2}):
1.0f-45 3.0f-45 4.0f-45 6.0f-45 7.0f-45
1f-45 3f-45 4f-45 6f-45 7f-45
```
"""
reinterpret(::Type{T}, x) where {T} = bitcast(T, x)
Expand Down
6 changes: 3 additions & 3 deletions base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Note the 2nd method allows passing in a byte buffer and position directly; calle
Various options for the output format include:
* `plus`: for positive `x`, prefix decimal string with a `'+'` character
* `space`: for positive `x`, prefix decimal string with a `' '` character; overridden if `plus=true`
* `hash`: whether the decimal point should be written, even if no additional digits are needed for precision
* `hash`: whether the decimal point should be written, even if no additional digits are needed for precision. Default is `nothing`, which will only force printing if no exponent is used.
* `precision`: minimum number of digits to be included in the decimal string; extra `'0'` characters will be added for padding if necessary
* `expchar`: character to use exponent component in scientific notation
* `padexp`: whether two digits should always be written, even for single-digit exponents (e.g. `e+1` becomes `e+01`)
Expand All @@ -38,7 +38,7 @@ Various options for the output format include:
function writeshortest(x::T,
plus::Bool=false,
space::Bool=false,
hash::Bool=true,
hash::Union{Bool,Nothing}=nothing,
precision::Integer=-1,
expchar::UInt8=UInt8('e'),
padexp::Bool=false,
Expand Down Expand Up @@ -112,7 +112,7 @@ function Base.show(io::IO, x::T, forceuntyped::Bool=false) where {T <: Base.IEEE
compact = get(io, :compact, false)
buf = Base.StringVector(neededdigits(T))
typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
pos = writeshortest(buf, 1, x, false, false, true, -1,
pos = writeshortest(buf, 1, x, false, false, nothing, -1,
x isa Float32 ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
write(io, resize!(buf, pos - 1))
return
Expand Down
10 changes: 8 additions & 2 deletions base/ryu/shortest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ end


@inline function writeshortest(buf::Vector{UInt8}, pos, x::T,
plus=false, space=false, hash=true,
plus=false, space=false, hash=nothing,
precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'),
typed=false, compact=false) where {T}
@assert 0 < pos <= length(buf)
Expand All @@ -245,7 +245,7 @@ end
end
buf[pos] = UInt8('0')
pos += 1
if hash
if hash === nothing || hash
buf[pos] = decchar
pos += 1
end
Expand Down Expand Up @@ -332,6 +332,9 @@ end
pt = nexp + olength
if -4 < pt <= (precision == -1 ? (T == Float16 ? 3 : 6) : precision)
exp_form = false
if hash === nothing
hash = true
end
if pt <= 0
buf[pos] = UInt8('0')
pos += 1
Expand All @@ -347,6 +350,9 @@ end
# nothing to do at this point
end
else
if hash === nothing
hash = false
end
pos += 1
end
i = 0
Expand Down
6 changes: 3 additions & 3 deletions doc/src/manual/integers-and-floating-point-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ julia> -1.23
-1.23
julia> 1e10
1.0e10
1e10
julia> 2.5e-4
0.00025
Expand Down Expand Up @@ -331,7 +331,7 @@ The underscore `_` can be used as digit separator:

```jldoctest
julia> 10_000, 0.000_000_005, 0xdead_beef, 0b1011_0010
(10000, 5.0e-9, 0xdeadbeef, 0xb2)
(10000, 5e-9, 0xdeadbeef, 0xb2)
```

### Floating-point zero
Expand Down Expand Up @@ -454,7 +454,7 @@ julia> eps(1e-27)
1.793662034335766e-43
julia> eps(0.0)
5.0e-324
5e-324
```

The distance between two adjacent representable floating-point numbers is not constant, but is
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Unicode names (in UTF-8 encoding) are allowed:

```jldoctest
julia> δ = 0.00001
1.0e-5
1e-5
julia> 안녕하세요 = "Hello"
"Hello"
Expand Down
36 changes: 18 additions & 18 deletions test/ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ end

@testset "MinAndMax" begin
@test "1.7976931348623157e308" == Ryu.writeshortest(Core.bitcast(Float64, 0x7fefffffffffffff))
@test "5.0e-324" == Ryu.writeshortest(Core.bitcast(Float64, Int64(1)))
@test "5e-324" == Ryu.writeshortest(Core.bitcast(Float64, Int64(1)))
end

@testset "LotsOfTrailingZeros" begin
Expand Down Expand Up @@ -158,16 +158,16 @@ end
@test "1000.0" == Ryu.writeshortest(1.0e+3)
@test "10000.0" == Ryu.writeshortest(1.0e+4)
@test "100000.0" == Ryu.writeshortest(1.0e+5)
@test "1.0e6" == Ryu.writeshortest(1.0e+6)
@test "1.0e7" == Ryu.writeshortest(1.0e+7)
@test "1.0e8" == Ryu.writeshortest(1.0e+8)
@test "1.0e9" == Ryu.writeshortest(1.0e+9)
@test "1.0e10" == Ryu.writeshortest(1.0e+10)
@test "1.0e11" == Ryu.writeshortest(1.0e+11)
@test "1.0e12" == Ryu.writeshortest(1.0e+12)
@test "1.0e13" == Ryu.writeshortest(1.0e+13)
@test "1.0e14" == Ryu.writeshortest(1.0e+14)
@test "1.0e15" == Ryu.writeshortest(1.0e+15)
@test "1e6" == Ryu.writeshortest(1.0e+6)
@test "1e7" == Ryu.writeshortest(1.0e+7)
@test "1e8" == Ryu.writeshortest(1.0e+8)
@test "1e9" == Ryu.writeshortest(1.0e+9)
@test "1e10" == Ryu.writeshortest(1.0e+10)
@test "1e11" == Ryu.writeshortest(1.0e+11)
@test "1e12" == Ryu.writeshortest(1.0e+12)
@test "1e13" == Ryu.writeshortest(1.0e+13)
@test "1e14" == Ryu.writeshortest(1.0e+14)
@test "1e15" == Ryu.writeshortest(1.0e+15)

# 10^15 + 10^i
@test "1.000000000000001e15" == Ryu.writeshortest(1.0e+15 + 1.0e+0)
Expand Down Expand Up @@ -240,14 +240,14 @@ end

@testset "MinAndMax" begin
@test "3.4028235e38" == Ryu.writeshortest(Core.bitcast(Float32, 0x7f7fffff))
@test "1.0e-45" == Ryu.writeshortest(Core.bitcast(Float32, Int32(1)))
@test "1e-45" == Ryu.writeshortest(Core.bitcast(Float32, Int32(1)))
end

# Check that we return the exact boundary if it is the shortest
# representation, but only if the original floating point number is even.
@testset "BoundaryRoundeven" begin
@test "3.355445e7" == Ryu.writeshortest(3.355445f7)
@test "9.0e9" == Ryu.writeshortest(8.999999f9)
@test "9e9" == Ryu.writeshortest(8.999999f9)
@test "3.436672e10" == Ryu.writeshortest(3.4366717f10)
end

Expand Down Expand Up @@ -284,15 +284,15 @@ end
@test "7.038531e-26" == Ryu.writeshortest(7.0385309f-26)
@test "9.223404e17" == Ryu.writeshortest(9.2234038f17)
@test "6.710887e7" == Ryu.writeshortest(6.7108872f7)
@test "1.0e-44" == Ryu.writeshortest(1.0f-44)
@test "1e-44" == Ryu.writeshortest(1.0f-44)
@test "2.816025e14" == Ryu.writeshortest(2.816025f14)
@test "9.223372e18" == Ryu.writeshortest(9.223372f18)
@test "1.5846086e29" == Ryu.writeshortest(1.5846085f29)
@test "1.1811161e19" == Ryu.writeshortest(1.1811161f19)
@test "5.368709e18" == Ryu.writeshortest(5.368709f18)
@test "4.6143166e18" == Ryu.writeshortest(4.6143165f18)
@test "0.007812537" == Ryu.writeshortest(0.007812537f0)
@test "1.0e-45" == Ryu.writeshortest(1.4f-45)
@test "1e-45" == Ryu.writeshortest(1.4f-45)
@test "1.18697725e20" == Ryu.writeshortest(1.18697724f20)
@test "1.00014165e-36" == Ryu.writeshortest(1.00014165f-36)
@test "200.0" == Ryu.writeshortest(200f0)
Expand Down Expand Up @@ -760,9 +760,9 @@ end # exp
@test stringcompact(0.025621074) == "0.0256211"

# subnormals
@test stringcompact(eps(0.0)) == "5.0e-324"
@test stringcompact(eps(0f0)) == "1.0f-45"
@test stringcompact(eps(Float16(0.0))) == "6.0e-8"
@test stringcompact(eps(0.0)) == "5e-324"
@test stringcompact(eps(0f0)) == "1f-45"
@test stringcompact(eps(Float16(0.0))) == "6e-8"
end

end # Ryu

0 comments on commit 1bae680

Please sign in to comment.