Given the divisor (normally a power of 10), the remainder = v % divisor for some number v and the error, returns whether v should be rounded up, down, or whether the rounding direction can't be determined due to error. error should be less than divisor / 2.
| 737 | // whether the rounding direction can't be determined due to error. |
| 738 | // error should be less than divisor / 2. |
| 739 | inline round_direction get_round_direction(uint64_t divisor, uint64_t remainder, |
| 740 | uint64_t error) { |
| 741 | FMT_ASSERT(remainder < divisor, ""); // divisor - remainder won't overflow. |
| 742 | FMT_ASSERT(error < divisor, ""); // divisor - error won't overflow. |
| 743 | FMT_ASSERT(error < divisor - error, ""); // error * 2 won't overflow. |
| 744 | // Round down if (remainder + error) * 2 <= divisor. |
| 745 | if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2) |
| 746 | return round_direction::down; |
| 747 | // Round up if (remainder - error) * 2 >= divisor. |
| 748 | if (remainder >= error && |
| 749 | remainder - error >= divisor - (remainder - error)) { |
| 750 | return round_direction::up; |
| 751 | } |
| 752 | return round_direction::unknown; |
| 753 | } |
| 754 | |
| 755 | namespace digits { |
| 756 | enum result { |