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