From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp62.i.mail.ru (smtp62.i.mail.ru [217.69.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 473E541C5DA for ; Wed, 24 Jun 2020 19:53:45 +0300 (MSK) From: Chris Sosnin Date: Wed, 24 Jun 2020 19:53:32 +0300 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v2 2/2] Add IsInt method for checking the fractional part of a number List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org, v.shpilevoy@tarantool.org Currently there is no efficient way to do this. Needed for tarantool/tarantool#4415 --- decNumber.c | 27 +++++++++++++++++++++++++++ decNumber.h | 1 + 2 files changed, 28 insertions(+) diff --git a/decNumber.c b/decNumber.c index e248656..26acec3 100644 --- a/decNumber.c +++ b/decNumber.c @@ -501,6 +501,33 @@ uLong decNumberToUInt64(const decNumber *dn, decContext *set) { return 0; } // decNumberToUInt64 +Flag decNumberIsInt(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; // work + // slice off fraction digits and check for non-zero + #if DECDPUN<=4 + rem=*up-QUOT10(*up, count)*powers[count]; + #else + rem=*up%powers[count]; // slice off discards + #endif + if (rem!=0) return 0; + } + } + return 1; + } // decNumberIsInt + /* ------------------------------------------------------------------ */ /* to-scientific-string -- conversion to numeric string */ /* to-engineering-string -- conversion to numeric string */ diff --git a/decNumber.h b/decNumber.h index ffaa3d8..9d3a7e1 100644 --- a/decNumber.h +++ b/decNumber.h @@ -169,6 +169,7 @@ decNumber * decNumberTrim(decNumber *); const char * decNumberVersion(void); decNumber * decNumberZero(decNumber *); + uint8_t decNumberIsInt(const decNumber *dn); /* Functions for testing decNumbers (normality depends on context) */ int32_t decNumberIsNormal(const decNumber *, decContext *); -- 2.21.1 (Apple Git-122.3)