diff options
Diffstat (limited to 'strings')
-rw-r--r-- | strings/decimal.c | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/strings/decimal.c b/strings/decimal.c index 224d75f6844..2dcc2c88ceb 100644 --- a/strings/decimal.c +++ b/strings/decimal.c @@ -901,19 +901,26 @@ int decimal_round(decimal *from, decimal *to, int scale, decimal_round_mode mode if (scale >= from->frac) goto done; /* nothing to do */ - DBUG_ASSERT(frac0+intg0 > 0); buf0+=intg0+frac0-1; buf1+=intg0+frac0-1; if (scale == frac0*DIG_PER_DEC1) { + DBUG_ASSERT(frac0+intg0 >= 0); x=buf0[1]/DIG_MASK; if (x > round_digit || - (round_digit == 5 && x == 5 && (mode == HALF_UP || *buf0 & 1))) - (*buf1)++; + (round_digit == 5 && x == 5 && (mode == HALF_UP || + (frac0+intg0 > 0 && *buf0 & 1)))) + { + if (frac0+intg0>0) + (*buf1)++; + else + *(++buf1)=DIG_BASE; + } } else { int pos=frac0*DIG_PER_DEC1-scale-1; + DBUG_ASSERT(frac0+intg0 > 0); x=*buf1 / powers10[pos]; y=x % 10; if (y > round_digit || @@ -942,7 +949,7 @@ int decimal_round(decimal *from, decimal *to, int scale, decimal_round_mode mode scale=frac0*DIG_PER_DEC1; error=E_DEC_TRUNCATED; /* XXX */ } - for (buf1=to->buf+frac0+intg0; buf1 > to->buf; buf1--) + for (buf1=to->buf+intg0+max(frac0,0); buf1 > to->buf; buf1--) { buf1[0]=buf1[-1]; } @@ -1233,6 +1240,16 @@ int decimal_cmp(decimal *from1, decimal *from2) return from1->sign > from2->sign ? -1 : 1; } +int decimal_is_zero(decimal *from) +{ + dec1 *buf1=from->buf, + *end=buf1+ROUND_UP(from->intg)+ROUND_UP(from->frac); + while (buf1 < end) + if (*buf1++) + return 0; + return 1; +} + /* multiply two decimals @@ -2103,6 +2120,9 @@ main() test_ro("15.4",-1,HALF_UP,"20"); test_ro("-15.4",-1,HALF_UP,"-20"); test_ro("5.4",-1,HALF_UP,"10"); + test_ro(".999", 0, HALF_UP, "1"); + memset(buf2, 33, sizeof(buf2)); + test_ro("999999999", -9, HALF_UP, "1000000000"); test_ro("15.1",0,HALF_EVEN,"15"); test_ro("15.5",0,HALF_EVEN,"16"); test_ro("14.5",0,HALF_EVEN,"14"); |