Double add double get wrong result

classic Classic list List threaded Threaded
2 messages Options
yangjiajun yangjiajun
Reply | Threaded
Open this post in threaded view
|

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/
ptupitsyn ptupitsyn
Reply | Threaded
Open this post in threaded view
|

Re: Double add double get wrong result

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.




On Tue, Jan 12, 2021 at 5:46 AM yangjiajun <[hidden email]> 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/