| 1 |
|
| 2 |
#include "fast_float/fast_float.h" |
| 3 |
|
| 4 |
#include <cassert> |
| 5 |
#include <cmath> |
| 6 |
#include <cstdio> |
| 7 |
#include <ios> |
| 8 |
#include <iostream> |
| 9 |
#include <limits> |
| 10 |
#include <system_error> |
| 11 |
|
| 12 |
template <typename T> char *to_string(T d, char *buffer) { |
| 13 |
auto written = std::snprintf(buffer, 64, "%.*e", |
| 14 |
std::numeric_limits<T>::max_digits10 - 1, d); |
| 15 |
return buffer + written; |
| 16 |
} |
| 17 |
|
| 18 |
void allvalues() { |
| 19 |
char buffer[64]; |
| 20 |
for (uint64_t w = 0; w <= 0xFFFFFFFF; w++) { |
| 21 |
float v; |
| 22 |
if ((w % 1048576) == 0) { |
| 23 |
std::cout << "."; |
| 24 |
std::cout.flush(); |
| 25 |
} |
| 26 |
uint32_t word = uint32_t(w); |
| 27 |
memcpy(&v, &word, sizeof(v)); |
| 28 |
|
| 29 |
{ |
| 30 |
char const *string_end = to_string(v, buffer); |
| 31 |
float result_value; |
| 32 |
auto result = fast_float::from_chars(buffer, string_end, result_value); |
| 33 |
// Starting with version 4.0 for fast_float, we return result_out_of_range |
| 34 |
// if the value is either too small (too close to zero) or too large |
| 35 |
// (effectively infinity). So std::errc::result_out_of_range is normal for |
| 36 |
// well-formed input strings. |
| 37 |
if (result.ec != std::errc() && |
| 38 |
result.ec != std::errc::result_out_of_range) { |
| 39 |
std::cerr << "parsing error ? " << buffer << std::endl; |
| 40 |
abort(); |
| 41 |
} |
| 42 |
if (std::isnan(v)) { |
| 43 |
if (!std::isnan(result_value)) { |
| 44 |
std::cerr << "not nan" << buffer << std::endl; |
| 45 |
abort(); |
| 46 |
} |
| 47 |
} else if (copysign(1, result_value) != copysign(1, v)) { |
| 48 |
std::cerr << "I got " << std::hexfloat << result_value |
| 49 |
<< " but I was expecting " << v << std::endl; |
| 50 |
abort(); |
| 51 |
} else if (result_value != v) { |
| 52 |
std::cerr << "no match ? " << buffer << std::endl; |
| 53 |
std::cout << "started with " << std::hexfloat << v << std::endl; |
| 54 |
std::cout << "got back " << std::hexfloat << result_value << std::endl; |
| 55 |
std::cout << std::dec; |
| 56 |
abort(); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
std::cout << std::endl; |
| 61 |
} |
| 62 |
|
| 63 |
int main() { |
| 64 |
allvalues(); |
| 65 |
std::cout << std::endl; |
| 66 |
std::cout << "all ok" << std::endl; |
| 67 |
return EXIT_SUCCESS; |
| 68 |
} |