[PATCH 1/5] decimal: fix decimal.round() when scale == 0
Serge Petrenko
sergepetrenko at tarantool.org
Wed Jul 17 18:33:42 MSK 2019
Fixes decimal.round() with zero scale, fixes an error with
decimal.round() when rounding leads to a number with the same
precision, for example, decimal.round(9.9, 0) -> 10.
Follow-up #692
---
src/lib/core/decimal.c | 8 ++++----
test/app/decimal.result | 18 ++++++++++++++++++
test/app/decimal.test.lua | 6 ++++++
3 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c
index 1b4f44362..1423ae418 100644
--- a/src/lib/core/decimal.c
+++ b/src/lib/core/decimal.c
@@ -172,14 +172,14 @@ decimal_round(decimal_t *dec, int scale)
if (scale < 0 || scale > DECIMAL_MAX_DIGITS)
return NULL;
- if (scale > decimal_scale(dec))
+ if (scale >= decimal_scale(dec))
return dec;
- int ndig = decimal_precision(dec) - decimal_scale(dec) + scale;
+ int ndig = MAX(decimal_precision(dec) - decimal_scale(dec) + scale, 1);
decContext context = {
ndig, /* Precision */
- ndig - 1, /* emax */
- -1, /* emin */
+ ndig, /* emax */
+ scale != 0 ? -1 : 0, /* emin */
DECIMAL_ROUNDING, /* rounding */
0, /* no traps */
0, /* zero status */
diff --git a/test/app/decimal.result b/test/app/decimal.result
index ecb189277..53ec73edc 100644
--- a/test/app/decimal.result
+++ b/test/app/decimal.result
@@ -458,3 +458,21 @@ a ^ 2.5
| ---
| - error: '[string "return a ^ 2.5 "]:1: decimal operation failed'
| ...
+
+-- check correct rounding when scale = 0
+decimal.round(decimal.new(0.9), 0)
+ | ---
+ | - '1'
+ | ...
+decimal.round(decimal.new(9.9), 0)
+ | ---
+ | - '10'
+ | ...
+decimal.round(decimal.new(99.9), 0)
+ | ---
+ | - '100'
+ | ...
+decimal.round(decimal.new(99.4), 0)
+ | ---
+ | - '99'
+ | ...
diff --git a/test/app/decimal.test.lua b/test/app/decimal.test.lua
index 4aff0f2a4..20fd4d81a 100644
--- a/test/app/decimal.test.lua
+++ b/test/app/decimal.test.lua
@@ -128,3 +128,9 @@ a = decimal.new('-13')
a ^ 2
-- fractional powers are allowed only for positive numbers
a ^ 2.5
+
+-- check correct rounding when scale = 0
+decimal.round(decimal.new(0.9), 0)
+decimal.round(decimal.new(9.9), 0)
+decimal.round(decimal.new(99.9), 0)
+decimal.round(decimal.new(99.4), 0)
--
2.20.1 (Apple Git-117)
More information about the Tarantool-patches
mailing list