From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Serge Petrenko Subject: [PATCH 1/5] decimal: fix decimal.round() when scale == 0 Date: Wed, 17 Jul 2019 18:33:42 +0300 Message-Id: <07a505e362ff037833e6501bf62a864b86694f91.1563376957.git.sergepetrenko@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: vdavydov.dev@gmail.com Cc: tarantool-patches@freelists.org, Serge Petrenko List-ID: 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)