Tarantool development patches archive
 help / color / mirror / Atom feed
* [PATCH] decimal: add modulo operator
@ 2019-08-09 12:41 Serge Petrenko
  2019-08-14 10:55 ` Vladimir Davydov
  0 siblings, 1 reply; 2+ messages in thread
From: Serge Petrenko @ 2019-08-09 12:41 UTC (permalink / raw)
  To: vdavydov.dev; +Cc: tarantool-patches, Serge Petrenko

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)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] decimal: add modulo operator
  2019-08-09 12:41 [PATCH] decimal: add modulo operator Serge Petrenko
@ 2019-08-14 10:55 ` Vladimir Davydov
  0 siblings, 0 replies; 2+ messages in thread
From: Vladimir Davydov @ 2019-08-14 10:55 UTC (permalink / raw)
  To: Serge Petrenko; +Cc: tarantool-patches

Pushed to master.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-08-14 10:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-09 12:41 [PATCH] decimal: add modulo operator Serge Petrenko
2019-08-14 10:55 ` Vladimir Davydov

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