fast_float
fast_float copied to clipboard
long double overload
Add long double
overload for from_chars()
to fully mimic std::from_chars
. See https://en.cppreference.com/w/cpp/utility/from_chars overload no.4
This is similar to issue https://github.com/fastfloat/fast_float/issues/86
You would probably want to introduce a whole distinct code sequence, akin to our current fallback.
Pull requests invited. Be mindful of the need to prove correctness.
Note that long double
is not a well defined type in practice. It appears that Visual Studio might map long double
to double
. And long double
could vary from standard binary128
to other variants.
It appears that long double
is system and compiler specific.
Given that fast_float
is compiler and system agnostic, it could prove difficult to support long double
.
But then, again, pull requests are invited.
Well, as pointed in the original post, there is a long double
overload in the C++ standard. And it is present in the standard regardless of the system/compiler.
I actually don't care at the moment about long double precision, I just care about the API to be more close to the standard, so that when from_chars(float/double/long double)
become widely supported by C++ standard library implementations it would be easier to switch to that. Hopefully, those implementations will adopt fast_float
implementation under the hood.
So, maybe in context of this issue we could just add long double
overload which will do double
under the hood?
Pseudocode:
from_chars(const char* str, long double& out){
double value;
from_chars(str, value);
out = (long double)(value);
}
Well, as pointed in the original post, there is a long double overload in the C++ standard. And it is present in the standard regardless of the system/compiler.
Yes, but that doesn't change anything about what Daniel said.
On Power systems long double
is double double which has a completely different representation from IEEE binary128. On x86 long double
is an 80-bit "extended precision" type, stored in a 128-bit object. Aarch64 uses binary128. On Windows long double
has identical representation to double
. So the point is that writing the code for long double
needs a lot more effort, and platform-specific knowledge.
Just using double
would give incorrect answers. If you're happy to get incorrect answers, you can just use the existing overload for double
.
On x86
long double
is an 80-bit "extended precision" type, stored in a 128-bit object.
Only on some (most?) x86 Linux. Both macOS and Windows have 64-bit long double
.
Ryu shows the peril of supporting this type. They have a generic_128
module to support up to 128-bit floats and provide a helper function for long double
, but omit any checks for the size of the type. So on macOS you simply get wrong answers.
Something that might make sense is the C++23 fixed-width floating-point types: std::float16_t
through std::float128_t
and std::bfloat16_t
.
https://en.cppreference.com/w/cpp/types/floating-point
@alugowski Agreed.