[Tarantool-patches] [PATCH v1 1/4] decimal: introduce decimal_is_neg()

imeevma at tarantool.org imeevma at tarantool.org
Mon Aug 16 18:57:00 MSK 2021


This patch introduces function decimal_is_neg() which checks that
decimal is less than zero.

Needed for #4415
---
 src/lib/core/decimal.c   |  6 ++++++
 src/lib/core/decimal.h   |  4 ++++
 test/unit/decimal.c      |  7 ++++++-
 test/unit/decimal.result | 10 +++++++++-
 4 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c
index 6d2ccb96f..7543ef44d 100644
--- a/src/lib/core/decimal.c
+++ b/src/lib/core/decimal.c
@@ -115,6 +115,12 @@ decimal_is_int(decimal_t *dec)
 	return decNumberIsInt(dec);
 }
 
+bool
+decimal_is_neg(const decimal_t *dec)
+{
+	return decNumberIsNegative(dec) && !decNumberIsZero(dec);
+}
+
 decimal_t *
 decimal_from_string(decimal_t *dec, const char *str)
 {
diff --git a/src/lib/core/decimal.h b/src/lib/core/decimal.h
index aeafd2c68..15a576648 100644
--- a/src/lib/core/decimal.h
+++ b/src/lib/core/decimal.h
@@ -81,6 +81,10 @@ decimal_zero(decimal_t *dec);
 bool
 decimal_is_int(decimal_t *dec);
 
+/** @return true if the decimal is negative, false otherwise. */
+bool
+decimal_is_neg(const decimal_t *dec);
+
 /**
  * Initialize a decimal with a value from the string.
  *
diff --git a/test/unit/decimal.c b/test/unit/decimal.c
index aea646e15..328b1b668 100644
--- a/test/unit/decimal.c
+++ b/test/unit/decimal.c
@@ -336,7 +336,7 @@ test_mp_print(void)
 int
 main(void)
 {
-	plan(304);
+	plan(312);
 
 	dectest(314, 271, uint64, uint64_t);
 	dectest(65535, 23456, uint64, uint64_t);
@@ -416,5 +416,10 @@ main(void)
 	dectest_is(is_int, 1.0000, true);
 	dectest_is(is_int, 1.0000001, false);
 
+	dectest_is(is_neg, 1, false);
+	dectest_is(is_neg, -1, true);
+	dectest_is(is_neg, 0, false);
+	dectest_is(is_neg, -0, false);
+
 	return check_plan();
 }
diff --git a/test/unit/decimal.result b/test/unit/decimal.result
index b7da2d2ce..ceaaf718a 100644
--- a/test/unit/decimal.result
+++ b/test/unit/decimal.result
@@ -1,4 +1,4 @@
-1..304
+1..312
 ok 1 - decimal(314)
 ok 2 - decimal(271)
 ok 3 - decimal(314) + decimal(271)
@@ -747,3 +747,11 @@ ok 301 - decimal_from_string(1.0000)
 ok 302 - decimal_is_int(1.0000) - expected true
 ok 303 - decimal_from_string(1.0000001)
 ok 304 - decimal_is_int(1.0000001) - expected false
+ok 305 - decimal_from_string(1)
+ok 306 - decimal_is_neg(1) - expected false
+ok 307 - decimal_from_string(-1)
+ok 308 - decimal_is_neg(-1) - expected true
+ok 309 - decimal_from_string(0)
+ok 310 - decimal_is_neg(0) - expected false
+ok 311 - decimal_from_string(-0)
+ok 312 - decimal_is_neg(-0) - expected false
-- 
2.25.1



More information about the Tarantool-patches mailing list