Skip to content

Commit

Permalink
AK: Add an exact and fast floating point parsing algorithm
Browse files Browse the repository at this point in the history
This is based on the paper by Daniel Lemire called
"Number parsing at a Gigabyte per second", currently available at
https://arxiv.org/abs/2101.11408
An implementation can be found at
https://github.com/fastfloat/fast_float

To support both strtod like methods and String::to_double we have two
different APIs. The parse_first_floating_point gives back both the
result, next character to read and the error/out of range status.
Out of range here means we rounded to infinity 0.

The other API, parse_floating_point_completely, will return a floating
point only if the given character range contains just the floating point
and nothing else. This can be much faster as we can skip actually
computing the value if we notice we did not parse the whole range.

Both of these APIs support a very lenient format to be usable in as many
places as possible. Also it does not check for "named" values like
"nan", "inf", "NAN" etc. Because this can be different for every usage.

For integers and small values this new method is not faster and often
even a tiny bit slower than the current strtod implementation. However
the strtod implementation is wrong for a lot of values and has a much
less predictable running time.

For correctness this method was tested against known string -> double
datasets from https://github.com/nigeltao/parse-number-fxx-test-data
This method gives 100% accuracy.
The old strtod gave an incorrect value in over 50% of the numbers
tested.
  • Loading branch information
davidot authored and linusg committed Oct 23, 2022
1 parent bf6d4a5 commit 53b7f5e
Show file tree
Hide file tree
Showing 5 changed files with 2,500 additions and 0 deletions.
1 change: 1 addition & 0 deletions AK/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(AK_SOURCES
Assertions.cpp
Base64.cpp
FloatingPointStringConversions.cpp
FlyString.cpp
Format.cpp
FuzzyMatch.cpp
Expand Down
Loading

0 comments on commit 53b7f5e

Please sign in to comment.