-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Switch float printing from grisu to ryu algorithm #32799
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened here? These are not the same floating point value:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore me, they are in Float32: