From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp56.i.mail.ru (smtp56.i.mail.ru [217.69.128.36]) (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 2F4CA469710 for ; Sun, 7 Jun 2020 15:40:34 +0300 (MSK) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.60.0.2.5\)) From: Chris Sosnin In-Reply-To: <6719bef9-feee-9f33-b569-acd5d1ca154f@tarantool.org> Date: Sun, 7 Jun 2020 15:40:32 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <48EB02AD-37B7-44AA-BF3C-6FCCF07FA33A@tarantool.org> References: <473ff051ab9075f28732c28396d4465460e4a130.1591371404.git.k.sosnin@tarantool.org> <6719bef9-feee-9f33-b569-acd5d1ca154f@tarantool.org> Subject: Re: [Tarantool-patches] [PATCH 2/2] Add IsWhole 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: Serge Petrenko Cc: tarantool-patches@dev.tarantool.org, Vladislav Shpilevoy Thank you for the review! > On 6 Jun 2020, at 17:17, Serge Petrenko = wrote: >=20 > 05.06.2020 18:41, Chris Sosnin =D0=BF=D0=B8=D1=88=D0=B5=D1=82: >> Currently there is no efficient way to do this. >>=20 >> Needed for tarantool/tarantool#4415 > Thanks for the patch! >> --- >> decNumber.c | 30 ++++++++++++++++++++++++++++++ >> decNumber.h | 1 + >> 2 files changed, 31 insertions(+) >>=20 >> 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=3Ddn->lsu; >> + if (dn->exponent>=3D0) { >> + return 1; >> + } >> + else { >> + Int count=3D-dn->exponent; >> + // spin up whole units until reach the Unit with the unit digit >> + for (; count>=3DDECDPUN; up++) { >> + if (*up!=3D0) return 0; >> + count-=3DDECDPUN; >> + } >> + if (count=3D=3D0) return 1; // [a multiple of = DECDPUN] >> + else { // [not multiple of DECDPUN] >> + Int rem, theInt=3D0; // work >> + // slice off fraction digits and check for non-zero >> + #if DECDPUN<=3D4 >> + theInt=3DQUOT10(*up, count); >> + rem=3D*up-theInt*powers[count]; >> + #else >> + rem=3D*up%powers[count]; // slice off discards >> + theInt=3D*up/powers[count]; >> + #endif >> + if (rem!=3D0) return 0; >> + up++; >> + } >> + } >> + return 1; >> + } // decNumberIsWhole >> + >=20 > Consider this diff: >=20 > diff --git a/decNumber.c b/decNumber.c > index f4ad927..acc3fb1 100644 > --- a/decNumber.c > +++ b/decNumber.c > @@ -515,17 +515,14 @@ Flag decNumberIsWhole(const decNumber *dn) { > } > if (count=3D=3D0) return 1; // [a multiple of = DECDPUN] > else { // [not multiple of DECDPUN] > - Int rem, theInt=3D0; // work > + Int rem; // work > // slice off fraction digits and check for non-zero > #if DECDPUN<=3D4 > - theInt=3DQUOT10(*up, count); > - rem=3D*up-theInt*powers[count]; > + rem=3D*up-QUOT10(*up, count)*powers[count]; > #else > rem=3D*up%powers[count]; // slice off discards > - theInt=3D*up/powers[count]; > #endif > if (rem!=3D0) return 0; > - up++; > } > } > return 1; I agree, looks much better, thanks! Applied on the branch. >=20 >> /* = ------------------------------------------------------------------ */ >> /* 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 *); >=20 > --=20 > Serge Petrenko