Skip to content

Commit 298b483

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Round debug float output to 4 decimal places
Summary: Float values in debug layout metrics output (used by Fantom test assertions) are subject to floating-point precision differences across platforms and compilers (e.g., `66.6667` vs `66.66666412353516`), causing flaky test comparisons. This changes `toString(const double&)` in `DebugStringConvertible.cpp` to format floats with 4 decimal places using `snprintf("%.4f")` and strip trailing zeros, replacing `double-conversion`'s `ToShortest()`. This is debug-only code (guarded by `#if RN_DEBUG_STRING_CONVERTIBLE`) so the precision change is safe for all consumers. The `double-conversion` dependency is also removed from the BUCK file since it is no longer used. Output examples: - `0.0` → `"0"` - `100.0` → `"100"` - `66.66666412353516` → `"66.6667"` - `1.5` → `"1.5"` - `33.333333` → `"33.3333"` Changelog: [Internal] Reviewed By: javache Differential Revision: D95041966
1 parent 422770d commit 298b483

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include <cinttypes>
1212
#include <cstdio>
1313

14-
#include <double-conversion/double-conversion.h>
15-
1614
namespace facebook::react {
1715

1816
#if RN_DEBUG_STRING_CONVERTIBLE
@@ -129,20 +127,20 @@ SharedDebugStringConvertibleList DebugStringConvertible::getDebugProps() const {
129127
* `toString`-family implementation.
130128
*/
131129
std::string toString(const double& value) {
132-
// Format taken from folly's toString
133-
static double_conversion::DoubleToStringConverter conv(
134-
0,
135-
"Infinity",
136-
"NaN",
137-
'E',
138-
-6, // detail::kConvMaxDecimalInShortestLow,
139-
21, // detail::kConvMaxDecimalInShortestHigh,
140-
6, // max leading padding zeros
141-
1); // max trailing padding zeros
142-
std::array<char, 256> buffer{};
143-
double_conversion::StringBuilder builder(buffer.data(), buffer.size());
144-
conv.ToShortest(value, &builder);
145-
return builder.Finalize();
130+
std::array<char, 64> buffer{};
131+
std::snprintf(buffer.data(), buffer.size(), "%.4f", value);
132+
std::string result(buffer.data());
133+
134+
// Strip anything that ends up being an integer after reducing precision
135+
if (auto dotPos = result.find('.'); dotPos != std::string::npos) {
136+
auto lastNonZero = result.find_last_not_of('0');
137+
if (lastNonZero == dotPos) {
138+
result.erase(dotPos);
139+
} else {
140+
result.erase(lastNonZero + 1);
141+
}
142+
}
143+
return result;
146144
}
147145

148146
std::string toString(const void* value) {

0 commit comments

Comments
 (0)