From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Serge Petrenko Subject: [PATCH] decimal: add modulo operator Date: Fri, 9 Aug 2019 15:41:22 +0300 Message-Id: <20190809124122.29614-1-sergepetrenko@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: vdavydov.dev@gmail.com Cc: tarantool-patches@freelists.org, Serge Petrenko List-ID: Part of #4403 @TarantoolBol document Title: Document decimal modulo operator There is now a modulo operator for decimal numbers: ``` a = decimal.new(172.51) a % 1 --- - '0.51' ... a % 0.3 --- - '0.01' ... a % 13.27 --- - '0.00' ... a % 173 --- - '172.51' ... a % 72 --- - '28.51' ... 720 % a --- - '29.96' ... ``` --- https://github.com/tarantool/tarantool/issues/4403 https://github.com/tarantool/tarantool/tree/sp/gh-4403-improve-decimals src/lib/core/decimal.c | 7 +++++++ src/lib/core/decimal.h | 6 ++++++ src/lua/decimal.c | 2 ++ test/app/decimal.result | 41 +++++++++++++++++++++++++++++++++++++++ test/app/decimal.test.lua | 12 ++++++++++++ 5 files changed, 68 insertions(+) diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c index 6ef351f81..e141a91f8 100644 --- a/src/lib/core/decimal.c +++ b/src/lib/core/decimal.c @@ -219,6 +219,13 @@ decimal_rescale(decimal_t *dec, int scale) return dec; } +decimal_t * +decimal_remainder(decimal_t *res, const decimal_t *lhs, const decimal_t *rhs) +{ + decNumberRemainder(res, lhs, rhs, &decimal_context); + return decimal_check_status(res, &decimal_context); +} + decimal_t * decimal_abs(decimal_t *res, const decimal_t *dec) { diff --git a/src/lib/core/decimal.h b/src/lib/core/decimal.h index 6e2cd3ce7..ffe49b652 100644 --- a/src/lib/core/decimal.h +++ b/src/lib/core/decimal.h @@ -141,6 +141,12 @@ decimal_trim(decimal_t *dec); decimal_t * decimal_rescale(decimal_t *dec, int scale); +/** + * res is set to the remainder of dividing lhs by rhs. + */ +decimal_t * +decimal_remainder(decimal_t *res, const decimal_t *lhs, const decimal_t *rhs); + /** * res is set to the absolute value of dec * decimal_abs(&a, &a) is allowed. diff --git a/src/lua/decimal.c b/src/lua/decimal.c index 330f4c3f8..de6586c8b 100644 --- a/src/lua/decimal.c +++ b/src/lua/decimal.c @@ -218,6 +218,7 @@ LDECIMAL_BINOP(sub, sub) LDECIMAL_BINOP(mul, mul) LDECIMAL_BINOP(div, div) LDECIMAL_BINOP(pow, pow) +LDECIMAL_BINOP(remainder, remainder) LDECIMAL_FUNC(log10, log10) LDECIMAL_FUNC(ln, ln) @@ -340,6 +341,7 @@ static const luaL_Reg ldecimal_mt[] = { {"__sub", ldecimal_sub}, {"__mul", ldecimal_mul}, {"__div", ldecimal_div}, + {"__mod", ldecimal_remainder}, {"__pow", ldecimal_pow}, {"__eq", ldecimal_eq}, {"__lt", ldecimal_lt}, diff --git a/test/app/decimal.result b/test/app/decimal.result index b53f4e75d..c632f57a7 100644 --- a/test/app/decimal.result +++ b/test/app/decimal.result @@ -528,3 +528,44 @@ decimal.round(decimal.new(99.4), 0) | --- | - '99' | ... + +-- check remainder operation +a = decimal.new(172.51) + | --- + | ... +a % 1 + | --- + | - '0.51' + | ... +a % 2 + | --- + | - '0.51' + | ... +a % 0.3 + | --- + | - '0.01' + | ... +a % 0.13 + | --- + | - '0.00' + | ... +a % 13.27 + | --- + | - '0.00' + | ... +a % 100 + | --- + | - '72.51' + | ... +a % 173 + | --- + | - '172.51' + | ... +a % 72 + | --- + | - '28.51' + | ... +720 % a + | --- + | - '29.96' + | ... diff --git a/test/app/decimal.test.lua b/test/app/decimal.test.lua index cee56d5e7..40f1f29de 100644 --- a/test/app/decimal.test.lua +++ b/test/app/decimal.test.lua @@ -149,3 +149,15 @@ 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) + +-- check remainder operation +a = decimal.new(172.51) +a % 1 +a % 2 +a % 0.3 +a % 0.13 +a % 13.27 +a % 100 +a % 173 +a % 72 +720 % a -- 2.20.1 (Apple Git-117)