Skip to content

Commit 563ebf1

Browse files
Abseil Teamcopybara-github
authored andcommitted
Fix double-promotion warnings in AppropriateResolution()
When -Wdouble-promotion is enabled, the templatized function AppropriateResolution fails to compile since its float instantiation promotes floats to doubles when doing arithmetic and comparisons. Add static casts to resolve these errors. PiperOrigin-RevId: 600776333 Change-Id: Ia530b4bbca6ddce27caf0a817196d87efef711cb
1 parent bd30c39 commit 563ebf1

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

googletest/include/gtest/gtest-printers.h

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124

125125
#if GTEST_INTERNAL_HAS_STD_SPAN
126126
#include <span> // NOLINT
127-
#endif // GTEST_INTERNAL_HAS_STD_SPAN
127+
#endif // GTEST_INTERNAL_HAS_STD_SPAN
128128

129129
namespace testing {
130130

@@ -241,8 +241,8 @@ struct StreamPrinter {
241241
// ADL (possibly involving implicit conversions).
242242
// (Use SFINAE via return type, because it seems GCC < 12 doesn't handle name
243243
// lookup properly when we do it in the template parameter list.)
244-
static auto PrintValue(const T& value, ::std::ostream* os)
245-
-> decltype((void)(*os << value)) {
244+
static auto PrintValue(const T& value,
245+
::std::ostream* os) -> decltype((void)(*os << value)) {
246246
// Call streaming operator found by ADL, possibly with implicit conversions
247247
// of the arguments.
248248
*os << value;
@@ -558,43 +558,50 @@ int AppropriateResolution(FloatType val) {
558558
#endif
559559
if (val < 1000000) {
560560
FloatType mulfor6 = 1e10;
561-
if (val >= 100000.0) { // 100,000 to 999,999
561+
// Without these static casts, the template instantiation for float would
562+
// fail to compile when -Wdouble-promotion is enabled, as the arithmetic and
563+
// comparison logic would promote floats to doubles.
564+
if (val >= static_cast<FloatType>(100000.0)) { // 100,000 to 999,999
562565
mulfor6 = 1.0;
563-
} else if (val >= 10000.0) {
566+
} else if (val >= static_cast<FloatType>(10000.0)) {
564567
mulfor6 = 1e1;
565-
} else if (val >= 1000.0) {
568+
} else if (val >= static_cast<FloatType>(1000.0)) {
566569
mulfor6 = 1e2;
567-
} else if (val >= 100.0) {
570+
} else if (val >= static_cast<FloatType>(100.0)) {
568571
mulfor6 = 1e3;
569-
} else if (val >= 10.0) {
572+
} else if (val >= static_cast<FloatType>(10.0)) {
570573
mulfor6 = 1e4;
571-
} else if (val >= 1.0) {
574+
} else if (val >= static_cast<FloatType>(1.0)) {
572575
mulfor6 = 1e5;
573-
} else if (val >= 0.1) {
576+
} else if (val >= static_cast<FloatType>(0.1)) {
574577
mulfor6 = 1e6;
575-
} else if (val >= 0.01) {
578+
} else if (val >= static_cast<FloatType>(0.01)) {
576579
mulfor6 = 1e7;
577-
} else if (val >= 0.001) {
580+
} else if (val >= static_cast<FloatType>(0.001)) {
578581
mulfor6 = 1e8;
579-
} else if (val >= 0.0001) {
582+
} else if (val >= static_cast<FloatType>(0.0001)) {
580583
mulfor6 = 1e9;
581584
}
582-
if (static_cast<FloatType>(static_cast<int32_t>(val * mulfor6 + 0.5)) /
585+
if (static_cast<FloatType>(static_cast<int32_t>(
586+
val * mulfor6 + (static_cast<FloatType>(0.5)))) /
583587
mulfor6 ==
584588
val)
585589
return 6;
586-
} else if (val < 1e10) {
587-
FloatType divfor6 = 1.0;
588-
if (val >= 1e9) { // 1,000,000,000 to 9,999,999,999
590+
} else if (val < static_cast<FloatType>(1e10)) {
591+
FloatType divfor6 = static_cast<FloatType>(1.0);
592+
if (val >= static_cast<FloatType>(1e9)) { // 1,000,000,000 to 9,999,999,999
589593
divfor6 = 10000;
590-
} else if (val >= 1e8) { // 100,000,000 to 999,999,999
594+
} else if (val >=
595+
static_cast<FloatType>(1e8)) { // 100,000,000 to 999,999,999
591596
divfor6 = 1000;
592-
} else if (val >= 1e7) { // 10,000,000 to 99,999,999
597+
} else if (val >=
598+
static_cast<FloatType>(1e7)) { // 10,000,000 to 99,999,999
593599
divfor6 = 100;
594-
} else if (val >= 1e6) { // 1,000,000 to 9,999,999
600+
} else if (val >= static_cast<FloatType>(1e6)) { // 1,000,000 to 9,999,999
595601
divfor6 = 10;
596602
}
597-
if (static_cast<FloatType>(static_cast<int32_t>(val / divfor6 + 0.5)) *
603+
if (static_cast<FloatType>(static_cast<int32_t>(
604+
val / divfor6 + (static_cast<FloatType>(0.5)))) *
598605
divfor6 ==
599606
val)
600607
return 6;

0 commit comments

Comments
 (0)