Tarantool development patches archive
 help / color / mirror / Atom feed
From: Chris Sosnin <k.sosnin@tarantool.org>
To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org,
	v.shpilevoy@tarantool.org
Subject: [Tarantool-patches] [PATCH 2/2] Add IsWhole method for checking the fractional part of a number
Date: Fri,  5 Jun 2020 18:41:13 +0300	[thread overview]
Message-ID: <473ff051ab9075f28732c28396d4465460e4a130.1591371404.git.k.sosnin@tarantool.org> (raw)
In-Reply-To: <cover.1591371404.git.k.sosnin@tarantool.org>

Currently there is no efficient way to do this.

Needed for tarantool/tarantool#4415
---
 decNumber.c | 30 ++++++++++++++++++++++++++++++
 decNumber.h |  1 +
 2 files changed, 31 insertions(+)

diff --git a/decNumber.c b/decNumber.c
index 7c53bc3..f4ad927 100644
--- a/decNumber.c
+++ b/decNumber.c
@@ -501,6 +501,36 @@ uLong decNumberToUInt64(const decNumber *dn, decContext *set) {
   return 0;
   }  // decNumberToUInt64
 
+Flag decNumberIsWhole(const decNumber *dn) {
+  const Unit *up=dn->lsu;
+  if (dn->exponent>=0) {
+    return 1;
+    }
+   else {
+    Int count=-dn->exponent;
+    // spin up whole units until reach the Unit with the unit digit
+    for (; count>=DECDPUN; up++) {
+      if (*up!=0) return 0;
+      count-=DECDPUN;
+      }
+    if (count==0) return 1;             // [a multiple of DECDPUN]
+     else {                             // [not multiple of DECDPUN]
+      Int rem, theInt=0;                          // work
+      // slice off fraction digits and check for non-zero
+      #if DECDPUN<=4
+        theInt=QUOT10(*up, count);
+        rem=*up-theInt*powers[count];
+      #else
+        rem=*up%powers[count];          // slice off discards
+        theInt=*up/powers[count];
+      #endif
+      if (rem!=0) return 0;
+      up++;
+      }
+    }
+    return 1;
+  }  // decNumberIsWhole
+
 /* ------------------------------------------------------------------ */
 /* to-scientific-string -- conversion to numeric string               */
 /* to-engineering-string -- conversion to numeric string              */
diff --git a/decNumber.h b/decNumber.h
index 85a3f3f..71ab27f 100644
--- a/decNumber.h
+++ b/decNumber.h
@@ -169,6 +169,7 @@
   decNumber  * decNumberTrim(decNumber *);
   const char * decNumberVersion(void);
   decNumber  * decNumberZero(decNumber *);
+  uint8_t      decNumberIsWhole(const decNumber *dn);
 
   /* Functions for testing decNumbers (normality depends on context)  */
   int32_t decNumberIsNormal(const decNumber *, decContext *);
-- 
2.21.1 (Apple Git-122.3)

  parent reply	other threads:[~2020-06-05 15:41 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-05 15:41 [Tarantool-patches] [PATCH 0/2] update decNumber for SQL Chris Sosnin
2020-06-05 15:41 ` [Tarantool-patches] [PATCH 1/2] Allow leading and trailing whitespaces in FromString Chris Sosnin
2020-06-06 13:48   ` Serge Petrenko
2020-06-07 12:38     ` Chris Sosnin
2020-06-11 17:06   ` Vladislav Shpilevoy
2020-06-11 20:14     ` Chris Sosnin
2020-06-05 15:41 ` Chris Sosnin [this message]
2020-06-06 14:17   ` [Tarantool-patches] [PATCH 2/2] Add IsWhole method for checking the fractional part of a number Serge Petrenko
2020-06-07 12:40     ` Chris Sosnin
2020-06-08  8:59       ` Serge Petrenko
2020-06-11 17:06   ` Vladislav Shpilevoy
2020-06-11 20:30     ` Chris Sosnin

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=473ff051ab9075f28732c28396d4465460e4a130.1591371404.git.k.sosnin@tarantool.org \
    --to=k.sosnin@tarantool.org \
    --cc=sergepetrenko@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 2/2] Add IsWhole method for checking the fractional part of a number' \
    /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