Fix gcc strict-overflow warning

Fix #566 #568
This commit is contained in:
Milo Yip 2016-03-02 01:01:17 +08:00
parent 6c927047c4
commit 928caf92ed

View File

@ -148,7 +148,7 @@ inline char* WriteExponent(int K, char* buffer) {
inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
const int kk = length + k; // 10^(kk-1) <= v < 10^kk
if (length <= kk && kk <= 21) {
if (0 <= k && kk <= 21) {
// 1234e7 -> 12340000000
for (int i = length; i < kk; i++)
buffer[i] = '0';
@ -160,7 +160,7 @@ inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
// 1234e-2 -> 12.34
std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
buffer[kk] = '.';
if (length > kk + maxDecimalPlaces) {
if (0 > k + maxDecimalPlaces) {
// When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1
// Remove extra trailing zeros (at least one) after truncation.
for (int i = kk + maxDecimalPlaces; i > kk + 1; i--)