Skip to content

Commit

Permalink
Switch float printing from grisu to ryu algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Aug 14, 2019
1 parent 6527de0 commit 869090b
Show file tree
Hide file tree
Showing 26 changed files with 2,148 additions and 78 deletions.
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Julia includes code from the following projects, which have their own licenses:

The following components included in Julia `Base` have their own separate licenses:

- base/ryu/* [Boost] (see [ryu](https://github.com/ulfjack/ryu/blob/master/LICENSE-Boost))
- base/grisu/* [BSD-3] (see [double-conversion](https://github.com/google/double-conversion/blob/master/LICENSE))
- base/special/{exp,rem_pio2,hyperbolic}.jl [Freely distributable with preserved copyright notice] (see [FDLIBM](https://www.netlib.org/fdlibm))

Expand Down
7 changes: 6 additions & 1 deletion base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,15 @@ function deepcopy_internal end
include("Enums.jl")
using .Enums

# BigInts and BigFloats
# BigInts
include("gmp.jl")
using .GMP

# float printing: requires BigInt
include("ryu/Ryu.jl")
using .Ryu

# BigFloats
include("mpfr.jl")
using .MPFR

Expand Down
4 changes: 2 additions & 2 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1059,10 +1059,10 @@ Create a `Float32` from `x`. If `x` is not exactly representable then `mode` det
# Examples
```jldoctest
julia> Float32(1/3, RoundDown)
0.3333333f0
0.3333333
julia> Float32(1/3, RoundUp)
0.33333334f0
0.33333334
```
See [`RoundingMode`](@ref) for available rounding modes.
Expand Down
6 changes: 3 additions & 3 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ julia> x = 1/3
0.3333333333333333
julia> convert(Float32, x)
0.33333334f0
0.33333334
julia> convert(Rational{Int32}, x)
1//3
Expand Down Expand Up @@ -402,11 +402,11 @@ For example,
# Examples
```jldoctest
julia> reinterpret(Float32, UInt32(7))
1.0f-44
1.0e-44
julia> reinterpret(Float32, UInt32[1 2 3 4 5])
1×5 reinterpret(Float32, ::Array{UInt32,2}):
1.4013e-45 2.8026e-45 4.2039e-45 5.60519e-45 7.00649e-45
1.0e-45 3.0e-45 4.0e-45 6.0e-45 7.0e-45
```
"""
reinterpret(::Type{T}, x) where {T} = bitcast(T, x)
Expand Down
6 changes: 3 additions & 3 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,10 @@ The highest finite value representable by the given floating-point DataType `T`.
# Examples
```jldoctest
julia> floatmax(Float16)
Float16(6.55e4)
65500.0
julia> floatmax(Float32)
3.4028235f38
3.4028235e38
```
"""
floatmax(x::T) where {T<:AbstractFloat} = floatmax(T)
Expand All @@ -790,7 +790,7 @@ julia> eps()
2.220446049250313e-16
julia> eps(Float32)
1.1920929f-7
1.1920929e-7
julia> 1.0 + eps()
1.0000000000000002
Expand Down
25 changes: 0 additions & 25 deletions base/grisu/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,6 @@ function _show(io::IO, x::AbstractFloat, mode, n::Int, typed, compact)
nothing
end

function Base.show(io::IO, x::Union{Float64,Float32})
if get(io, :compact, false)
_show(io, x, PRECISION, 6, x isa Float64, true)
else
_show(io, x, SHORTEST, 0, get(io, :typeinfo, Any) !== typeof(x), false)
end
end

function Base.show(io::IO, x::Float16)
hastypeinfo = Float16 === get(io, :typeinfo, Any)
# if hastypeinfo, the printing would be more compact using `SHORTEST`
# while still retaining all the information
# BUT: we want to print all digits in `show`, not in display, so we rely
# on the :compact property to make the decision
# (cf. https://github.com/JuliaLang/julia/pull/24651#issuecomment-345535687)
if get(io, :compact, false) && !hastypeinfo
_show(io, x, PRECISION, 5, false, true)
else
_show(io, x, SHORTEST, 0, !hastypeinfo, false)
end
end

Base.print(io::IO, x::Float32) = _show(io, x, SHORTEST, 0, false, false)
Base.print(io::IO, x::Float16) = _show(io, x, SHORTEST, 0, false, false)

# normal:
# 0 < pt < len ####.#### len+1
# pt <= 0 0.000######## len-pt+1
Expand Down
4 changes: 2 additions & 2 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,10 @@ The lowest value representable by the given (real) numeric DataType `T`.
# Examples
```jldoctest
julia> typemin(Float16)
-Inf16
-Inf
julia> typemin(Float32)
-Inf32
-Inf
```
"""
function typemin end
Expand Down
2 changes: 1 addition & 1 deletion base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ If no arguments can be converted, an error is raised.
# Examples
```jldoctest
julia> promote(Int8(1), Float16(4.5), Float32(4.1))
(1.0f0, 4.5f0, 4.1f0)
(1.0, 4.5, 4.1)
```
"""
function promote end
Expand Down
25 changes: 25 additions & 0 deletions base/ryu/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
The code in this directory (base/ryu) is a derivative based on the work in the https://github.com/ulfjack/ryu repository, which allows the use of the Boost software license, included below.

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
99 changes: 99 additions & 0 deletions base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module Ryu

include("utils.jl")
include("shortest.jl")
include("fixed.jl")
include("exp.jl")

neededdigits(::Type{Float64}) = 309 + 17
neededdigits(::Type{Float32}) = 39 + 9
neededdigits(::Type{Float16}) = 9 + 5

"""
Ryu.writeshortest(x, plus=false, space=false, hash=true, precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'))
Ryu.writeshortest(buf::Vector{UInt8}, pos::Int, x, args...)
Convert a float value `x` into its "shortest" decimal string, which can be parsed back to the same value.
This function allows achieving the `%g` printf format.
Note the 2nd method allows passing in a byte buffer and position directly; callers must ensure the buffer has sufficient room to hold the entire decimal string.
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
* `precision`: minimum number of significant 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`)
* `decchar`: decimal point character to be used
"""
function writeshortest(x::T,
plus::Bool=false,
space::Bool=false,
hash::Bool=true,
precision::Integer=-1,
expchar::UInt8=UInt8('e'),
padexp::Bool=false,
decchar::UInt8=UInt8('.')) where {T <: Base.IEEEFloat}
buf = Base.StringVector(neededdigits(T))
pos = writeshortest(buf, 1, x)
@assert pos - 1 <= length(buf)
return String(resize!(buf, pos - 1))
end

"""
Ryu.writefixed(x, plus=false, space=false, hash=true, precision=-1, decchar=UInt8('.'))
Ryu.writefixed(buf::Vector{UInt8}, pos::Int, x, args...)
Convert a float value `x` into a "fixed" size decimal string.
This function allows achieving the `%f` printf format.
Note the 2nd method allows passing in a byte buffer and position directly; callers must ensure the buffer has sufficient room to hold the entire decimal string.
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
* `precision`: minimum number of significant digits to be included in the decimal string; extra `'0'` characters will be added for padding if necessary
* `decchar`: decimal point character to be used
"""
function writefixed(x::T, precision) where {T <: Base.IEEEFloat}
buf = Base.StringVector(precision + neededdigits(T))
pos = writefixed(buf, 1, x, false, false, false, precision)
@assert pos - 1 <= length(buf)
return String(resize!(buf, pos - 1))
end

"""
Ryu.writeexp(x, plus=false, space=false, hash=true, precision=-1, expchar=UInt8('e'), decchar=UInt8('.'))
Ryu.writeexp(buf::Vector{UInt8}, pos::Int, x, args...)
Convert a float value `x` into a scientific notation decimal string.
This function allows achieving the `%e` printf format.
Note the 2nd method allows passing in a byte buffer and position directly; callers must ensure the buffer has sufficient room to hold the entire decimal string.
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
* `precision`: minimum number of significant 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
* `decchar`: decimal point character to be used
"""
function writeexp(x::T, precision) where {T <: Base.IEEEFloat}
buf = Base.StringVector(precision + neededdigits(T))
pos = writeexp(buf, 1, x, false, false, false, precision)
@assert pos - 1 <= length(buf)
return String(resize!(buf, pos - 1))
end

function Base.show(io::IO, x::T) where {T <: Base.IEEEFloat}
if get(io, :compact, false)
x = round(x, sigdigits=6)
end
buf = Base.StringVector(neededdigits(T))
pos = writeshortest(buf, 1, x)
@assert pos - 1 <= length(buf)
write(io, resize!(buf, pos - 1))
return
end

end # module
Loading

0 comments on commit 869090b

Please sign in to comment.