Tarantool development patches archive
 help / color / mirror / Atom feed
From: Chris Sosnin <k.sosnin@tarantool.org>
To: tarantool-patches@dev.tarantool.org, v.shpilevoy@tarantool.org
Subject: [Tarantool-patches] [PATCH] decimal: introduce decimal_is_int
Date: Fri, 26 Jun 2020 14:58:43 +0300	[thread overview]
Message-ID: <20200626115843.68530-1-k.sosnin@tarantool.org> (raw)
In-Reply-To: <070b81c4-c752-01ef-c004-4d768260809d@tarantool.org>

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)

  reply	other threads:[~2020-06-26 11:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-25 14:23 [Tarantool-patches] [PATCH 0/2] decimal: intrdoduce strtodec and IsInt Chris Sosnin
2020-06-25 14:23 ` [Tarantool-patches] [PATCH 1/2] decNumber: bump new version Chris Sosnin
2020-06-25 14:23 ` [Tarantool-patches] [PATCH 2/2] decimal: introduce strtodec function Chris Sosnin
2020-06-25 21:03 ` [Tarantool-patches] [PATCH 0/2] decimal: intrdoduce strtodec and IsInt Vladislav Shpilevoy
2020-06-26 11:58   ` Chris Sosnin [this message]
2020-06-28 16:53     ` [Tarantool-patches] [PATCH] decimal: introduce decimal_is_int Vladislav Shpilevoy
2020-07-02 10:25       ` Chris Sosnin
2020-07-02 20:11 ` [Tarantool-patches] [PATCH 0/2] decimal: intrdoduce strtodec and IsInt Vladislav Shpilevoy

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=20200626115843.68530-1-k.sosnin@tarantool.org \
    --to=k.sosnin@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH] decimal: introduce decimal_is_int' \
    /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