From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: [tarantool-patches] Re: [PATCH] decimal: add methods truncate and set_scale From: Serge Petrenko In-Reply-To: <20190719115746.GA29078@atlas> Date: Fri, 19 Jul 2019 15:15:11 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: References: <20190719112136.23483-1-sergepetrenko@tarantool.org> <20190719115746.GA29078@atlas> To: Konstantin Osipov Cc: Vladimir Davydov , tarantool-patches@freelists.org List-ID: > 19 =D0=B8=D1=8E=D0=BB=D1=8F 2019 =D0=B3., =D0=B2 14:57, Konstantin = Osipov =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0= =B0): >=20 > * Serge Petrenko [19/07/19 14:42]: >> This patch adds 2 methods to decimal library and lua module: >> truncate will remove all trailing fractional zeros and set_scale will >> either perform rounding or append excess fractional zeros. >>=20 >=20 > Please note that it is Trim in the ldec library. > truncate() has a slightly different meaning. >=20 >> +decimal_t * >> +decimal_truncate(decimal_t *dec) >> +{ >> + decimal_t *res =3D decNumberTrim(dec); >> + >> + /* No errors are possible */ >> + assert(res =3D=3D dec); >> + return res; >> +} >> + >> +decimal_t * >> +decimal_set_scale(decimal_t *dec, int scale) >> +{ >> + if (scale < 0) >> + return NULL; >> + if (scale <=3D decimal_scale(dec)) >> + return decimal_round(dec, scale); >> + /* how much zeros shoud we append. */ >> + int delta =3D scale + dec->exponent; >> + if (scale > DECIMAL_MAX_DIGITS || dec->digits + delta > = DECIMAL_MAX_DIGITS) >> + return NULL; >> + decimal_t new_scale; >> + decimal_from_int64(&new_scale, -scale); >> + decNumberRescale(dec, dec, &new_scale, &decimal_context); >> + assert(decimal_check_status(dec, &decimal_context) !=3D NULL); >> + return dec; >=20 > Please also note here: the name is rescale(), not set_scale(). The > meaning of set_scale() is slightly different. >=20 > Please use less confusing names. Hi! Thank you for review. No problem, renamed to trim() and rescale(). The changes are on the branch. >=20 > otherwise lgtm. >=20 > --=20 > Konstantin Osipov, Moscow, Russia >=20 -- Serge Petrenko sergepetrenko@tarantool.org