[Tarantool-patches] [PATCH] decimal: introduce decimal_is_int

Chris Sosnin k.sosnin at tarantool.org
Fri Jun 26 14:58:43 MSK 2020


Thank you for the review!
For some reason I thought we don't need a wrapper for a function
that doesn't require context and the status check. So does it mean
I should also wrap isZero and similar calls in my SQL patchset?

I added the following commit with the wrapper and tests on the branch:
=====================================================================

This function will be used to determine, whether we can safely
convert to integer without an information loss.

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

diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c
index e762266c5..95e809e62 100644
--- a/src/lib/core/decimal.c
+++ b/src/lib/core/decimal.c
@@ -109,6 +109,12 @@ decimal_zero(decimal_t *dec)
 	return dec;
 }
 
+bool
+decimal_is_int(decimal_t *dec)
+{
+	return decNumberIsInt(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 992fbd267..a48ec723f 100644
--- a/src/lib/core/decimal.h
+++ b/src/lib/core/decimal.h
@@ -36,6 +36,7 @@
 #define DECNUMDIGITS DECIMAL_MAX_DIGITS
 #include "third_party/decNumber/decNumber.h"
 #include <stdint.h>
+#include <stdbool.h>
 
 #if defined(__cplusplus)
 extern "C" {
@@ -65,6 +66,13 @@ decimal_scale(const decimal_t *dec);
 decimal_t *
 decimal_zero(decimal_t *dec);
 
+/**
+ * @return true if the fractional part of the number is 0,
+ * false otherwise.
+ */
+bool
+decimal_is_int(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 cf74de313..32694b88a 100644
--- a/test/unit/decimal.c
+++ b/test/unit/decimal.c
@@ -73,6 +73,13 @@
 	is(decimal_##op(&b, &a), NULL, "decimal_"#op"("#stra") - error on wrong operands.");\
 })
 
+#define dectest_is(op, stra, expect) ({\
+	decimal_t a;\
+	is(decimal_from_string(&a, #stra), &a, "decimal_from_string("#stra")");\
+	is(decimal_##op(&a), expect, "decimal_"#op"("#stra") - expected "\
+				      #expect);\
+})
+
 #define test_strtodec(str, end, expect) ({\
 	decimal_t dec;\
 	const char *endptr;\
@@ -326,7 +333,7 @@ test_mp_print(void)
 int
 main(void)
 {
-	plan(298);
+	plan(304);
 
 	dectest(314, 271, uint64, uint64_t);
 	dectest(65535, 23456, uint64, uint64_t);
@@ -402,5 +409,9 @@ main(void)
 	test_strtodec("NaN", 'N', failure);
 	test_strtodec("inf", 'i', failure);
 
+	dectest_is(is_int, 1, true);
+	dectest_is(is_int, 1.0000, true);
+	dectest_is(is_int, 1.0000001, false);
+
 	return check_plan();
 }
diff --git a/test/unit/decimal.result b/test/unit/decimal.result
index 8be8ea122..8f6de61a7 100644
--- a/test/unit/decimal.result
+++ b/test/unit/decimal.result
@@ -1,4 +1,4 @@
-1..298
+1..304
 ok 1 - decimal(314)
 ok 2 - decimal(271)
 ok 3 - decimal(314) + decimal(271)
@@ -723,3 +723,9 @@ ok 295 - strtodec("NaN") failure
 ok 296 - strtodec("NaN") - expected end of valid string at 'N'
 ok 297 - strtodec("inf") failure
 ok 298 - strtodec("inf") - expected end of valid string at 'i'
+ok 299 - decimal_from_string(1)
+ok 300 - decimal_is_int(1) - expected true
+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
-- 
2.21.1 (Apple Git-122.3)



More information about the Tarantool-patches mailing list