Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In cassandra when to use decimal Vs float/double?

I am using apache cassandra 3.x version. I am bit confused regarding when should I use decimal vs float types?

Is there any specific use-cases/differences when should go for float or avoid decimal and vice-versa?

I have gone through some quick tutorial none covered this difference. can anyone help me understand this ?

like image 281
BdEngineer Avatar asked Oct 18 '19 12:10

BdEngineer


People also ask

What is the difference between float and decimal and double?

The float value ranges from approximately ±1.5e-45 to ±3.4e38. The double value ranges from approximately ±5.0e-324 to ±1.7e308. The Decimal value ranges from approximately ±1.0e-28 to ±7.9e28. Float represent data with single precision. Double represent data with double precision. Decimal has higher precision than float and Double.

What is a BLOB data type in Cassandra?

Cassandra blob data type represents a constant hexadecimal number. Use a tuple as an alternative to a user-defined type.

What is the range of the float and decimals?

The float value ranges from approximately ±1.5e-45 to ±3.4e38. The double value ranges from approximately ±5.0e-324 to ±1.7e308. The Decimal value ranges from approximately ±1.0e-28 to ±7.9e28.

What is a non-frozen type in Cassandra?

Non-frozen types allow updates to individual fields. Cassandra treats the value of a frozen type as a blob. The entire value must be overwritten. A collection of one or more ordered elements: [literal, literal, literal]. Lists have limitations and specific performance considerations. Use a frozen list to decrease impact.


2 Answers

From the book Learning Apache Cassandra By Mat Brown:

Cassandra has three types that store non-integer numbers:

  • The float type stores 32-bit IEEE-754 floating point numbers.
  • The double type stores 64-bit IEEE-754 floating point numbers.
  • The decimal type stores variable-precision decimal numbers, with no upper bound on size. Unlike a floating point number, a variable-precision decimal will never suffer from base 10 rounding errors in the fractional part of the number.

But decimal is likely to take up more space compared to other two. So, if it is a matter of precision, you can go for decimal. Otherwise, float/double are good enough in most of the cases.

like image 115
sazzad Avatar answered Oct 20 '22 07:10

sazzad


My observation was that float at max holds value 5 digits after decimal beyond that it approximates it. Ex if you have 1.00001 it would get inserted as it is but if you have 1.000001 it's inserted as 1 . It rounds up based on precision. If you have clear idea about about mantisa and precision (how many digits before and how many digits after decimal ) you can go with decimal . (Float and double behave the same way) .

like image 2
aryan singh Avatar answered Oct 20 '22 07:10

aryan singh