-
-
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
[WIP] use jl_strtof_c to convert strings to float #49699
base: master
Are you sure you want to change the base?
Conversation
src/flisp/read.c
Outdated
|
||
// NOTE(#49689): DO NOT convert double to float directly, | ||
// indirect conversion (string->double->float) will lose precision. | ||
f = jl_strtof_c(tok, &end); |
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.
This call (plus the call to jl_strtod_c
above) can be sunk inside the conditional.
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.
I moved jl_strtof_c
into the branch of if (pval)
.
But left jl_strtod_c
untouched.
Lines 42 to 46 in e204e20
d = jl_strtod_c(tok, &end); | |
if (*end == '\0') { | |
if (pval) *pval = mk_double(fl_ctx, d); | |
return 1; | |
} |
Calling
jl_strtod_c
updates char *end
before we can do dereference *end
.So I don't think we can move
jl_strtod_c
.
Somewhat unrelatedly, I recently considered vendor-ing a copy of gdtoa from mingw, with the locale support disabled. It is only about 250k of code, so probably quite reasonable to consider. This led me on a journey to update the copy of gdtoa in mingw upstream itself first though, which has led to discovering that gdtoa had introduced a new bug in strtod("nan"). Anyways, the current state of that work is here: https://github.com/JuliaLang/julia/commits/jn/import-strtod |
My next plan was to use |
This one is significantly worse quality, and does not conform to the current C standards. |
What about LLVM's libc implementation. It doesn't look super complicated (It's c++ but doesn't seem to use any super fancy C++isms) https://github.com/llvm/llvm-project/blob/c13ed1cc75781fccc7cec91017e84341c3047229/libc/src/__support/str_to_float.h#L1079 |
Apparently that implementation should be much (up to 10x) faster than gdtoa used elsewhere, by virtue of being more modern (and failing occasionally): the design for that llvm code is apparently from https://nigeltao.github.io/blog/2020/eisel-lemire.html#testing Seems worthwhile to vendor that C++ code if we can from LLVM. As mentioned there, this algorithm also exists already in Julia: https://github.com/JuliaData/Parsers.jl/blob/589b9d0f80998ec284874b300da0932557d33513/src/floats.jl#L349 |
indirect conversion (string->double->float) will lose precision
indirect conversion (string->double->float) will lose precision
Do we still need this fix (for flisp)? The rounding of
|
We may still need it for some of the default parse implementations in base. I would hope copying in that llvm code would work well enough that we could just do that and vendor a working copy of the algorithm that way with minimal fuss. Though maybe we could also just import that existing Julia code, and avoid calling into C at all for this? |
ref: #49689
This pr tries to fix the precision problem when rounding doubles to floats on *nix platforms.
The rounding problem on the windows platform will be fixed in the next pr.
indirect conversion (string->double->float) will lose precision
before pr
Version 1.10.0-DEV.1255 (2023-05-10) Commit e204e20
julia --lisp
julia
after pr
julia --lisp
(float xxxx)
?julia
17.328679084777833f0