Print __float128, without using quadmath_snprintf
c, c++, floating-point, floating-point-precision, precision
Solution
`__float128` is a vendor extension.
In C23, `_FloatN` for `N = 16, 32, 64, 128` or multiples of 32 greater than 128 are standardised extensions that a compiler can provide. So `_Float128` would be the standard type. To print the type, you use `strfromfN` to turn it into a string:
void print(_Float128 fp) {
char buf[32 + sizeof(".e+99999")];
int sz = strfromf128(buf, sizeof buf, "%.32g", fp);
fwrite(buf, 1, sz, stdout);
}
https://godbolt.org/z/E7W6a4r8d
In C++23, as well as `_FloatN` probably being provided as well by your compilers as an extension, `std::float128_t` is provided as the standard 128-bit float type.
It is implementation defined if there is an overload for `operator<<` so you can simply use `std::cout`, but there will be a `std::formatter` specialization, so `std::format` can be used to create a string:
void print(std::float128_t fp) {
std::println("{:.32g}", fp); // std::println is C++26
// C++23: `std::cout << std::format("{:.32g}\n", fp);`
}
https://godbolt.org/z/5sahahn4P
Before C23/C++23, there was no standard type for 128 bit float. Sometimes `long double` is 128 bits, but that is rare (it is more likely to be 80 bits or the same 64 bits as a regular `double`). So you had to use compiler extensions or a software emulation library.
Problem
In my question about Analysis of float/double precision in 32 decimal digits, one answer said to take a look at `__float128`. I used it and the compiler could find it, but I can not print it, since the complier can not find the header `quadmath.h`. So my questions are: - `__float128` is standard, correct? - How to print it? - Isn't `quadmath.h` standard? These answers did not help: - Use `extern C` - Precision in C++ - Printing The ref also did not help. Note that I do not want to use any non standard library. [EDIT] It would be also useful, if that question had an answer, even if the answer was a negative one.