You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by yangjiajun <13...@qq.com> on 2021/01/12 02:46:11 UTC

Double add double get wrong result

Hello.I do simple add operation but get wrong result.

Here is a reproduce step:

CREATE TABLE PUBLIC.TEST (
	A DOUBLE,
	B DOUBLE,
	K VARCHAR,
	CONSTRAINT K PRIMARY KEY (K)
);

INSERT INTO test(A,B,K)values(-11674.46,11674.67,'1')

SELECT to_char(A+B) FROM test

The result:.21000000000094587.But it obviously should be .21.

Do u have any ideas about this?



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Re: Double add double get wrong result

Posted by Pavel Tupitsyn <pt...@apache.org>.
Hello,

You'll get the same results in plain Java code

      double x = -11674.46;
      double y = 11674.67;
      System.out.println(x + y);

This is caused by the internal floating point representation [1] [2]
You can try DECIMAL instead of DOUBLE.


[1]
https://stackoverflow.com/questions/322749/retain-precision-with-double-in-java
[2] https://en.wikipedia.org/wiki/Floating-point_arithmetic


On Tue, Jan 12, 2021 at 5:46 AM yangjiajun <13...@qq.com> wrote:

> Hello.I do simple add operation but get wrong result.
>
> Here is a reproduce step:
>
> CREATE TABLE PUBLIC.TEST (
>         A DOUBLE,
>         B DOUBLE,
>         K VARCHAR,
>         CONSTRAINT K PRIMARY KEY (K)
> );
>
> INSERT INTO test(A,B,K)values(-11674.46,11674.67,'1')
>
> SELECT to_char(A+B) FROM test
>
> The result:.21000000000094587.But it obviously should be .21.
>
> Do u have any ideas about this?
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
>