Tarantool development patches archive
 help / color / mirror / Atom feed
From: Serge Petrenko <sergepetrenko@tarantool.org>
To: vdavydov.dev@gmail.com
Cc: tarantool-patches@freelists.org,
	Serge Petrenko <sergepetrenko@tarantool.org>
Subject: [PATCH] decimal: add modulo operator
Date: Fri,  9 Aug 2019 15:41:22 +0300	[thread overview]
Message-ID: <20190809124122.29614-1-sergepetrenko@tarantool.org> (raw)

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)

             reply	other threads:[~2019-08-09 12:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-09 12:41 Serge Petrenko [this message]
2019-08-14 10:55 ` Vladimir Davydov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190809124122.29614-1-sergepetrenko@tarantool.org \
    --to=sergepetrenko@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH] decimal: add modulo operator' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox