Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra float precision

I created float value from a Cassandra table with a float type column.

CREATE TABLE testing(
id int,
value float,
primary key (id)
);

INSERT INTO testing(id,  value) VALUES (1, 2341.1);  

SELECT * from testing;

I get that the value is 2341.1001

What's wrong with my setting? Thanks.

Here is my cassandra version.

cassandra@cqlsh> show version
[cqlsh 5.0.1 | Cassandra 3.11.1 | CQL spec 3.4.4 | Native protocol v4]
like image 318
user3458437 Avatar asked Oct 23 '25 17:10

user3458437


1 Answers

floats & doubles are represented in memory as close approximations to number that you specified as input. You can find more details about internal representation of floats and doubles in the Java language specification.

If you want to have precise numbers you can use decimal type, that is mapped into Java's BigDecimal type, but it use more memory/disk space.

like image 141
Alex Ott Avatar answered Oct 25 '25 09:10

Alex Ott