[Tarantool-patches] [PATCH 2/2] Add IsWhole method for checking the fractional part of a number
Serge Petrenko
sergepetrenko at tarantool.org
Mon Jun 8 11:59:09 MSK 2020
07.06.2020 15:40, Chris Sosnin пишет:
> Thank you for the review!
>
>> On 6 Jun 2020, at 17:17, Serge Petrenko <sergepetrenko at tarantool.org> wrote:
>>
>> 05.06.2020 18:41, Chris Sosnin пишет:
>>> Currently there is no efficient way to do this.
>>>
>>> Needed for tarantool/tarantool#4415
>> Thanks for the patch!
>>> ---
>>> 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
>>> +
>> Consider this diff:
>>
>> 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==0) return 1; // [a multiple of DECDPUN]
>> else { // [not multiple of DECDPUN]
>> - Int rem, theInt=0; // work
>> + Int rem; // work
>> // slice off fraction digits and check for non-zero
>> #if DECDPUN<=4
>> - theInt=QUOT10(*up, count);
>> - rem=*up-theInt*powers[count];
>> + rem=*up-QUOT10(*up, count)*powers[count];
>> #else
>> rem=*up%powers[count]; // slice off discards
>> - theInt=*up/powers[count];
>> #endif
>> if (rem!=0) return 0;
>> - up++;
>> }
>> }
>> return 1;
> I agree, looks much better, thanks!
> Applied on the branch.
Thanks for the fixes! Both patches LGTM.
>
>>> /* ------------------------------------------------------------------ */
>>> /* 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 *);
>> --
>> Serge Petrenko
--
Serge Petrenko
More information about the Tarantool-patches
mailing list