Skip to content

Commit aed3ddd

Browse files
Tomas Dzetkuliccopybara-github
authored andcommitted
Improve NaN handling in absl::Duration arithmetic.
PiperOrigin-RevId: 774936932 Change-Id: Ibde499b8c9825b4357edf71cfcb9c45eb75f4702
1 parent 4e5beaf commit aed3ddd

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

absl/time/duration.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ Duration& Duration::operator*=(int64_t r) {
469469

470470
Duration& Duration::operator*=(double r) {
471471
if (time_internal::IsInfiniteDuration(*this) || !IsFinite(r)) {
472-
const bool is_neg = std::signbit(r) != (rep_hi_.Get() < 0);
472+
const bool is_neg = std::isnan(r) || std::signbit(r) != (rep_hi_.Get() < 0);
473473
return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
474474
}
475475
return *this = ScaleDouble<std::multiplies>(*this, r);
@@ -485,7 +485,7 @@ Duration& Duration::operator/=(int64_t r) {
485485

486486
Duration& Duration::operator/=(double r) {
487487
if (time_internal::IsInfiniteDuration(*this) || !IsValidDivisor(r)) {
488-
const bool is_neg = std::signbit(r) != (rep_hi_.Get() < 0);
488+
const bool is_neg = std::isnan(r) || std::signbit(r) != (rep_hi_.Get() < 0);
489489
return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
490490
}
491491
return *this = ScaleDouble<std::divides>(*this, r);

absl/time/duration_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,18 +841,18 @@ TEST(Duration, DivisionByZero) {
841841

842842
TEST(Duration, NaN) {
843843
// Note that IEEE 754 does not define the behavior of a nan's sign when it is
844-
// copied, so the code below allows for either + or - InfiniteDuration.
844+
// copied. We return -InfiniteDuration in either case.
845845
#define TEST_NAN_HANDLING(NAME, NAN) \
846846
do { \
847847
const auto inf = absl::InfiniteDuration(); \
848848
auto x = NAME(NAN); \
849-
EXPECT_TRUE(x == inf || x == -inf); \
849+
EXPECT_TRUE(x == -inf); \
850850
auto y = NAME(42); \
851851
y *= NAN; \
852-
EXPECT_TRUE(y == inf || y == -inf); \
852+
EXPECT_TRUE(y == -inf); \
853853
auto z = NAME(42); \
854854
z /= NAN; \
855-
EXPECT_TRUE(z == inf || z == -inf); \
855+
EXPECT_TRUE(z == -inf); \
856856
} while (0)
857857

858858
const double nan = std::numeric_limits<double>::quiet_NaN();

0 commit comments

Comments
 (0)