Fast and exact implementation of the C++ from_chars functions for number types: 4x to 10x faster than strtod, part of GCC 12, MySQL, DuckDB, Chromium, Redis and
Fast and exact implementation of the C++ from_chars functions for number types: 4x to 10x faster than strtod, part of GCC 12, MySQL, DuckDB, Chromium, Redis and
Note: This library is for C++ users. C programmers should consider ffc.h. It is a high-performance port of fast_float to C.
The fast_float library provides fast header-only implementations for the C++
from_chars functions for float and double types as well as integer types.
These functions convert ASCII strings representing decimal values (e.g.,
1.3e10) into binary types. We provide exact rounding (including round to
even). In our experience, these fast_float functions many times faster than
comparable number-parsing functions from existing C++ standard libraries.
Specifically, fast_float provides the following two functions to parse
floating-point numbers with a C++17-like syntax (the library itself only
requires C++11):
from_chars_result from_chars(char const *first, char const *last, float &value, ...);
from_chars_result from_chars(char const *first, char const *last, double &value, ...);If they are available on your system, we also support fixed-width floating-point types such as std::float64_t, std::float32_t, std::float16_t, and std::bfloat16_t.
You can also parse integer types such as char, short, long, long long, unsigned char, unsigned short, unsigned long, unsigned long long, bool (0/1), int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t.
from_chars_result from_chars(char const *first, char const *last, int &value, ...);
from_chars_result from_chars(char const *first, char const *last, unsigned &value, ...);The return type (from_chars_result) is defined as the struct:
struct from_chars_result {
char const *ptr;
std::errc ec;
};It parses the character sequence [first, last) for a number. It parses
floating-point numbers expecting a locale-independent format equivalent to the
C++17 from_chars function. The resulting floating-point value is the closest
floating-point values (using either float or double), using the "round to
even" convention for values that would otherwise fall right in-between two
values. That is, we provide exact parsing according to the IEEE standard.
Given a successful parse, the pointer (ptr) in the returned value is set to
point right after the parsed number, and the value referenced is set to the
parsed value. In case of error, the returned ec contains a representative
error, otherwise the default (std::errc()) value is stored.
The implementation does not throw and does not allocate memory (e.g., with new
or malloc).
It will parse infinity and nan values.
Example:
#include "fast_float/fast_float.h"
#include <iostream>
#include <string>
int main() {
std::string input = "3.1416 xyz ";
double result;
auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result);
if (answer.ec != std::errc()) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; }
std::cout << "parsed the number " << result << std::endl;
return EXIT_SUCCESS;
}Prior to C++26, checking for a successful std::from_chars conversion requires comparing the from_chars_result::ec member to std::errc(). As an extension fast_float::from_chars supports the improved C++26 API that allows checking the result by converting it to bool, like so:
#include "fast_float/fast_float.h"
#include <iostream>
#include <string>
int main() {
std::string input = "3.1416 xyz ";
double result;
if(auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result)) {
std::cout << "parsed the number " << result << std::endl;
return EXIT_SUCCESS;
}
std::cerr << "failed to parse " << input << std::endl;
return EXIT_FAILURE;
}You can parse delimited numbers:
…Like the C++17 standard, the fast_float::from_chars functions take an optional
last argument of the type fast_float::chars_format. It is a bitset value: we
check whether fmt & fast_float::chars_format::fixed and fmt & fast_float::chars_format::scientific are set to determine whether we allow the
fixed point and scientific notation respectively. The default is
fast_float::chars_format::general which allows both fixed and scientific.
The library seeks to follow the C++17 (see 28.2.3.(6.1)) specification.
from_chars function does not skip leading white-space characters (unless
fast_float::chars_format::skip_white_space is set).+ sign is
forbidden (unless fast_float::chars_format::allow_leading_plus is set).float and double types). We seek the nearest value.
We round to an even mantissa when we are in-between two binary floating-point
numbers.Furthermore, we have the following restrictions:
float and double, but not long double. We also support
fixed-width floating-point types such as std::float64_t, std::float32_t,
std::float16_t, and std::bfloat16_t.1e9999), we
represent them using a positive or negative infinity and the returned
ec is set to std::errc::result_out_of_range.1e-9999), we represent them
using a positive or negative zero and the returned ec is set to
std::errc::result_out_of_range.We support Visual Studio, macOS, Linux, freeBSD. We support big and little endian. We support 32-bit and 64-bit systems.
We assume that the rounding mode is set to nearest (std::fegetround() == FE_TONEAREST).
You can also parse integer types using different bases (e.g., 2, 10, 16). The following code will print the number 22250738585072012 three times:
…When parsing floating-point values, the numbers can sometimes be too small
(e.g., 1e-1000) or too large (e.g., 1e1000). The C language established the
precedent that these small values are out of range. In such cases, it is
customary to parse small values to zero and large values to infinity. That is
the behaviour of the C language (e.g., strtod). That is the behaviour followed
by the fast_float library.
Specifically, we follow Jonathan Wakely's interpretation of the standard:
In any case, the resulting value is one of at most two floating-point values closest to the value of the string matching the pattern.
It is also the approach taken by the Microsoft C++ library.
Hence, we have the following examples:
double result = -1;
std::string str = "3e-1000";
auto r = fast_float::from_chars(str.data(), str.data() + str.size(), result);
// r.ec == std::errc::result_out_of_range
// r.ptr == str.data() + 7
// result == 0 double result = -1;
std::string str = "3e1000";
auto r = fast_float::from_chars(str.data(), str.data() + str.size(), result);
// r.ec == std::errc::result_out_of_range
// r.ptr == str.data() + 6
// result == std::numeric_limits<double>::infinity()Users who wish for the value to be left unmodified given
std::errc::result_out_of_range may do so by adding two lines of code:
double old_result = result; // make copy
auto r = fast_float::from_chars(start, end, result);
if (r.ec == std::errc::result_out_of_range) { result = old_result; }In C++20, you may use fast_float::from_chars to parse strings at compile-time,
as in the following example:
// consteval forces compile-time evaluation of the function in C++20.
consteval double parse(std::string_view input) {
double result;
auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result);
if (answer.ec != std::errc()) { return -1.0; }
return result;
}
// This function should compile to a function which
// merely returns 3.1415.
constexpr double constexptest() {
return parse("3.1415 input");
}The library also supports fixed-width floating-point types such as
std::float64_t, std::float32_t, std::float16_t, and std::bfloat16_t.
E.g., you can write:
std::float32_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);We also support UTF-16 and UTF-32 inputs, as well as ASCII/UTF-8, as in the following example:
#include "fast_float/fast_float.h"
#include <iostream>
int main() {
std::u16string input = u"3.1416 xyz ";
double result;
auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result);
if (answer.ec != std::errc()) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; }
std::cout << "parsed the number " << result << std::endl;
return EXIT_SUCCESS;
}The C++ standard stipulate that from_chars has to be locale-independent. In
particular, the decimal separator has to be the period (.). However, some
users still want to use the fast_float library with in a locale-dependent
manner. Using a separate function called from_chars_advanced, we allow the
users to pass a parse_options instance which contains a custom decimal
separator (e.g., the comma). You may use it as follows.
#include "fast_float/fast_float.h"
#include <iostream>
int main() {
std::string input = "3,1416 xyz ";
double result;
fast_float::parse_options options{fast_float::chars_format::general, ','};
auto answer = fast_float::from_chars_advanced(input.data(), input.data() + input.size(), result, options);
if ((answer.ec != std::errc()) || ((result != 3.1416))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; }
std::cout << "parsed the number " << result << std::endl;
return EXIT_SUCCESS;
}#include "fast_float/fast_float.h"
#include <iostream>
int main() {
std::string input = "1d+4";
double result;
fast_float::parse_options options{fast_float::chars_format::fortran};
auto answer = fast_float::from_chars_advanced(input.data(), input.data() + input.size(), result, options);
if ((answer.ec != std::errc()) || ((result != 10000))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; }
std::cout << "parsed the number " << result << std::endl;
return EXIT_SUCCESS;
}#include "fast_float/fast_float.h"
#include <iostream>
int main() {
std::string input = "+.1"; // not valid
double result;
fast_float::parse_options options{fast_float::chars_format::json};
auto answer = fast_float::from_chars_advanced(input.data(), input.data() + input.size(), result, options);
if (answer.ec == std::errc()) { std::cerr << "should have failed\n"; return EXIT_FAILURE; }
return EXIT_SUCCESS;
}By default the JSON format does not allow inf:
#include "fast_float/fast_float.h"
#include <iostream>
int main() {
std::string input = "inf"; // not valid in JSON
double result;
fast_float::parse_options options{fast_float::chars_format::json};
auto answer = fast_float::from_chars_advanced(input.data(), input.data() + input.size(), result, options);
if (answer.ec == std::errc()) { std::cerr << "should have failed\n"; return EXIT_FAILURE; }
return EXIT_SUCCESS;
}You can allow it with a non-standard json_or_infnan variant:
#include "fast_float/fast_float.h"
#include <iostrNo open issues yet, or sync has not completed.