[tarantool-patches] [PATCH] decimal: add methods truncate and set_scale

Konstantin Osipov kostja at tarantool.org
Fri Jul 19 14:57:46 MSK 2019


* Serge Petrenko <sergepetrenko at tarantool.org> [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.
> 

Please note that it is Trim in the ldec library.
truncate() has a slightly different meaning.

> +decimal_t *
> +decimal_truncate(decimal_t *dec)
> +{
> +	decimal_t *res = decNumberTrim(dec);
> +
> +	/* No errors are possible */
> +	assert(res == dec);
> +	return res;
> +}
> +
> +decimal_t *
> +decimal_set_scale(decimal_t *dec, int scale)
> +{
> +	if (scale < 0)
> +		return NULL;
> +	if (scale <= decimal_scale(dec))
> +		return decimal_round(dec, scale);
> +	/* how much zeros shoud we append. */
> +	int delta = 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) != NULL);
> +	return dec;

Please also note here: the name is rescale(), not set_scale(). The
meaning of set_scale() is slightly different.

Please use less confusing names.

otherwise lgtm.

-- 
Konstantin Osipov, Moscow, Russia



More information about the Tarantool-patches mailing list