Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH 1/1] tuple: don't truncate float in :update()
Date: Sun, 26 Jan 2020 21:45:54 +0300	[thread overview]
Message-ID: <20200126184554.GD1144@tarantool.org> (raw)
In-Reply-To: <2dec35136a47362584f27ea3293bef0b1ad09af2.1580057028.git.v.shpilevoy@tarantool.org>

On 26 Jan 17:44, Vladislav Shpilevoy wrote:
> Before the patch there were the rules:
> * float +/- double = double
> * double +/- double = double
> * float +/- float = float
> 
> The rules were applied regardless of values. That led to a problem
> when float + float exceeding maximal float value could fit into
> double, but was stored as an infinity.
> 
> The patch makes so that if a floating point arithmetic operation
> result fits into float, it is stored as float. Otherwise as
> double. Regardless of initial types.
> 
> This alongside saves some memory for cases when doubles can be
> stored as floats, and therefore takes 4 less bytes. Although
> these cases are rare, because any not integer value stored in a
> double may have a long garbage tail in its fraction.
> 
> Closes #4701
> ---
> Branch: https://github.com/tarantool/tarantool/tree/gerold103/gh-4701-update-float-truncate
> Issue: https://github.com/tarantool/tarantool/issues/4701
> 
> I am not sure about the patch correctness. Perhaps we should not
> save double + double as float even when it fits. It would break
> DOUBLE data type, which we are going to introduce, because from
> what I understood, it is going to store MP_DOUBLE only.
> 
> On the other hand, DOUBLE is not implemented yet

I guess double is already on board. See d8193eb1c

> and when it will
> be implemented, we may decide to allow to store MP_FLOAT there.
> 
>  src/box/xrow_update_field.c | 18 ++++++++--
>  test/box/update.result      | 66 +++++++++++++++++++++++++++++++++++++
>  test/box/update.test.lua    | 46 ++++++++++++++++++++++++++
>  3 files changed, 127 insertions(+), 3 deletions(-)
> 
> diff --git a/src/box/xrow_update_field.c b/src/box/xrow_update_field.c
> index 7c0f5fb5e..31429ee37 100644
> --- a/src/box/xrow_update_field.c
> +++ b/src/box/xrow_update_field.c
> @@ -400,13 +400,25 @@ xrow_update_arith_make(struct xrow_update_op *op,
>  			unreachable();
>  			break;
>  		}
> -		if (lowest_type == XUPDATE_TYPE_DOUBLE) {
> +		float fc = (float) c;
> +		/*
> +		 * A value may be saved as double even if it looks
> +		 * like fitting a float. For example, 0.01 + 0.01
> +		 * may be stored as double. This is because
> +		 * 0.01 may be stored as 0.009999999999999, what
> +		 * looks like double precision. And there is no
> +		 * way how to check if this is actually 0.01.
> +		 * By the same reason FLT_MAX can't be used to
> +		 * detect whether a value fits float, because it
> +		 * may be <= FLT_MAX, but may have a double
> +		 * precision in its fraction part.
> +		 */
> +		if (c != (double) fc) {
>  			ret->type = XUPDATE_TYPE_DOUBLE;
>  			ret->dbl = c;
>  		} else {
> -			assert(lowest_type == XUPDATE_TYPE_FLOAT);
>  			ret->type = XUPDATE_TYPE_FLOAT;
> -			ret->flt = (float)c;
> +			ret->flt = fc;
>  		}
>  	} else {
>  		decimal_t a, b, c;

  reply	other threads:[~2020-01-26 18:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-26 16:44 Vladislav Shpilevoy
2020-01-26 18:45 ` Nikita Pettik [this message]
2020-01-27 21:56   ` Vladislav Shpilevoy
2020-02-23 14:11 Vladislav Shpilevoy
2020-02-24 17:57 ` Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200126184554.GD1144@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 1/1] tuple: don'\''t truncate float in :update()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox