templates and types

This commit is contained in:
Anders Dalvander 2024-12-03 23:47:21 +01:00
parent ac453a091a
commit 63bbefad6b

View File

@ -56,15 +56,18 @@
#define FASTFLOAT_ODDPLATFORM 1 #define FASTFLOAT_ODDPLATFORM 1
#endif #endif
#define iHexAndDec(v) \ template <typename T> std::string iHexAndDec(T v) {
(std::ostringstream{} << std::hex << "0x" << (v) << " (" << std::dec << (v) \ std::ostringstream ss;
<< ")") \ ss << std::hex << "0x" << (v) << " (" << std::dec << (v) << ")";
.str() return ss.str();
#define fHexAndDec(v) \ }
(std::ostringstream{} << std::hexfloat << (v) << " (" << std::defaultfloat \
<< std::setprecision(DBL_MAX_10_EXP + 1) << (v) \ template <typename T> std::string fHexAndDec(T v) {
<< ")") \ std::ostringstream ss;
.str() ss << std::hexfloat << (v) << " (" << std::defaultfloat
<< std::setprecision(DBL_MAX_10_EXP + 1) << (v) << ")";
return ss.str();
}
char const *round_name(int d) { char const *round_name(int d) {
switch (d) { switch (d) {
@ -174,7 +177,7 @@ TEST_CASE("double.parse_zero") {
auto r1 = fast_float::from_chars(zero, zero + 1, f); auto r1 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r1.ec == std::errc()); CHECK(r1.ec == std::errc());
std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -184,7 +187,7 @@ TEST_CASE("double.parse_zero") {
auto r2 = fast_float::from_chars(zero, zero + 1, f); auto r2 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r2.ec == std::errc()); CHECK(r2.ec == std::errc());
std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -194,7 +197,7 @@ TEST_CASE("double.parse_zero") {
auto r3 = fast_float::from_chars(zero, zero + 1, f); auto r3 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r3.ec == std::errc()); CHECK(r3.ec == std::errc());
std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -204,7 +207,7 @@ TEST_CASE("double.parse_zero") {
auto r4 = fast_float::from_chars(zero, zero + 1, f); auto r4 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r4.ec == std::errc()); CHECK(r4.ec == std::errc());
std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -226,7 +229,7 @@ TEST_CASE("double.parse_negative_zero") {
CHECK(r1.ec == std::errc()); CHECK(r1.ec == std::errc());
std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f) std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -237,7 +240,7 @@ TEST_CASE("double.parse_negative_zero") {
CHECK(r2.ec == std::errc()); CHECK(r2.ec == std::errc());
std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f) std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -248,7 +251,7 @@ TEST_CASE("double.parse_negative_zero") {
CHECK(r3.ec == std::errc()); CHECK(r3.ec == std::errc());
std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f) std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -259,7 +262,7 @@ TEST_CASE("double.parse_negative_zero") {
CHECK(r4.ec == std::errc()); CHECK(r4.ec == std::errc());
std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f) std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.);
::memcpy(&float64_parsed, &f, sizeof(f)); ::memcpy(&float64_parsed, &f, sizeof(f));
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed) std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
<< std::endl; << std::endl;
@ -312,7 +315,7 @@ TEST_CASE("float.parse_zero") {
auto r1 = fast_float::from_chars(zero, zero + 1, f); auto r1 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r1.ec == std::errc()); CHECK(r1.ec == std::errc());
std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;
@ -322,7 +325,7 @@ TEST_CASE("float.parse_zero") {
auto r2 = fast_float::from_chars(zero, zero + 1, f); auto r2 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r2.ec == std::errc()); CHECK(r2.ec == std::errc());
std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;
@ -332,7 +335,7 @@ TEST_CASE("float.parse_zero") {
auto r3 = fast_float::from_chars(zero, zero + 1, f); auto r3 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r3.ec == std::errc()); CHECK(r3.ec == std::errc());
std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;
@ -342,7 +345,7 @@ TEST_CASE("float.parse_zero") {
auto r4 = fast_float::from_chars(zero, zero + 1, f); auto r4 = fast_float::from_chars(zero, zero + 1, f);
CHECK(r4.ec == std::errc()); CHECK(r4.ec == std::errc());
std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl; std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;
@ -364,7 +367,7 @@ TEST_CASE("float.parse_negative_zero") {
CHECK(r1.ec == std::errc()); CHECK(r1.ec == std::errc());
std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f) std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;
@ -375,7 +378,7 @@ TEST_CASE("float.parse_negative_zero") {
CHECK(r2.ec == std::errc()); CHECK(r2.ec == std::errc());
std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f) std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;
@ -386,7 +389,7 @@ TEST_CASE("float.parse_negative_zero") {
CHECK(r3.ec == std::errc()); CHECK(r3.ec == std::errc());
std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f) std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;
@ -397,7 +400,7 @@ TEST_CASE("float.parse_negative_zero") {
CHECK(r4.ec == std::errc()); CHECK(r4.ec == std::errc());
std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f) std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f)
<< std::endl; << std::endl;
CHECK(f == 0); CHECK(f == 0.f);
::memcpy(&float32_parsed, &f, sizeof(f)); ::memcpy(&float32_parsed, &f, sizeof(f));
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed) std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
<< std::endl; << std::endl;