Skip to content

Commit 497d69f

Browse files
NickGerlemanmeta-codesync[bot]
authored andcommitted
Round debug float output to 4 decimal places (#55879)
Summary: Pull Request resolved: #55879 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 8cbc4dc commit 497d69f

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
#include "DebugStringConvertible.h"
99

10-
#include <array>
11-
#include <cinttypes>
12-
#include <cstdio>
13-
14-
#include <double-conversion/double-conversion.h>
10+
#include <cstdint>
11+
#include <iomanip>
12+
#include <sstream>
1513

1614
namespace facebook::react {
1715

@@ -129,33 +127,30 @@ 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::ostringstream stream;
131+
stream << std::fixed << std::setprecision(4) << value;
132+
std::string result = stream.str();
133+
134+
// Strip trailing zeros and unnecessary decimal point
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) {
149147
if (value == nullptr) {
150148
return "null";
151149
}
152-
std::array<char, 20> buffer{};
153-
std::snprintf(
154-
buffer.data(),
155-
buffer.size(),
156-
"0x%" PRIXPTR,
157-
reinterpret_cast<uintptr_t>(value));
158-
return buffer.data();
150+
std::ostringstream stream;
151+
stream << "0x" << std::uppercase << std::hex
152+
<< reinterpret_cast<uintptr_t>(value);
153+
return stream.str();
159154
}
160155

161156
#endif

0 commit comments

Comments
 (0)