Skip to content

Commit

Permalink
Deduplicate trailingZeros logic.
Browse files Browse the repository at this point in the history
This makes it clearer that negative and non-negative `rexp` behave
identically except for an additional criterion in the negative case.
  • Loading branch information
StephanTLavavej authored and ulfjack committed Apr 3, 2019
1 parent 2a5de23 commit 46bf5cf
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions ryu/d2fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,15 +710,14 @@ int d2exp_buffered_n(double d, uint32_t precision, char* result) {
roundUp = lastDigit > 5;
} else {
// Is m * 2^e2 * 10^(precision + 1 - exp) integer?
bool trailingZeros;
// precision was already increased by 1, so we don't need to write + 1 here.
const int32_t rexp = (int32_t) precision - exp;
const int32_t requiredTwos = -e2 - rexp;
bool trailingZeros = requiredTwos <= 0
|| (requiredTwos < 60 && multipleOfPowerOf2(m2, (uint32_t) requiredTwos));
if (rexp < 0) {
const int32_t requiredFives = -rexp;
trailingZeros = (requiredTwos <= 0 || (requiredTwos < 60 && multipleOfPowerOf2(m2, (uint32_t) requiredTwos))) && multipleOfPowerOf5(m2, (uint32_t) requiredFives);
} else {
trailingZeros = requiredTwos <= 0 || (requiredTwos < 60 && multipleOfPowerOf2(m2, (uint32_t) requiredTwos));
trailingZeros = trailingZeros && multipleOfPowerOf5(m2, (uint32_t) requiredFives);
}
roundUp = trailingZeros ? 2 : 1;
#ifdef RYU_DEBUG
Expand Down

0 comments on commit 46bf5cf

Please sign in to comment.