Fixed toString(double) not picking the closest digits for 17-digit values. (#474)

This commit is contained in:
Branimir Karadžić
2026-10-23 22:29:37 -07:00
committed by GitHub
parent d0ce13617d
commit b366b69f74
2 changed files with 25 additions and 4 deletions
+23 -4
View File
@@ -203,9 +203,28 @@ namespace bx
'9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'
};
static const uint32_t s_kPow10[] =
static const uint64_t s_kPow10[] =
{
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
UINT64_C(1),
UINT64_C(10),
UINT64_C(100),
UINT64_C(1000),
UINT64_C(10000),
UINT64_C(100000),
UINT64_C(1000000),
UINT64_C(10000000),
UINT64_C(100000000),
UINT64_C(1000000000),
UINT64_C(10000000000),
UINT64_C(100000000000),
UINT64_C(1000000000000),
UINT64_C(10000000000000),
UINT64_C(100000000000000),
UINT64_C(1000000000000000),
UINT64_C(10000000000000000),
UINT64_C(100000000000000000),
UINT64_C(1000000000000000000),
UINT64_C(10000000000000000000),
};
DiyFp GetCachedPower(int32_t e, int32_t* K)
@@ -289,7 +308,7 @@ namespace bx
if (tmp <= delta)
{
*K += kappa;
GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(s_kPow10[kappa]) << -one.e, wp_w.f);
GrisuRound(buffer, *len, delta, tmp, s_kPow10[kappa] << -one.e, wp_w.f);
return;
}
}
@@ -311,7 +330,7 @@ namespace bx
{
*K += kappa;
const int index = -static_cast<int>(kappa);
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? s_kPow10[-static_cast<int>(kappa)] : 0));
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < int32_t(BX_COUNTOF(s_kPow10) ) ? s_kPow10[index] : 0));
return;
}
}
+2
View File
@@ -421,6 +421,8 @@ TEST_CASE("toString double", "[string]")
REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
REQUIRE(testToString(-1.234567e-9, "-1.234567e-9") );
REQUIRE(testToString(0.1 + 0.2, "0.30000000000000004") );
REQUIRE(testToString(1.0000000000000002, "1.0000000000000002") );
}
template<typename Ty>