In hive, when doing below select statement, it is returning 1.20000000000000002 as result instead of 1.2.
select 1.1 + 0.1;
In reality, it is doing the same thing within our hive tables when storing a similar FLOAT value so I want to ask on this. Thanks
That is how floating point arithmetic works. If you need precision, cast
the result as a decimal
.
select cast(1.1+0.1 as decimal(9,1))
Decimal literals provide precise values and greater range for floating point numbers than the DOUBLE type. Decimal data types store exact representations of numeric values, while DOUBLE data types store very close approximations of numeric values.
Decimal types are needed for use cases in which the (very close) approximation of a DOUBLE is insufficient, such as financial applications, equality and inequality checks, and rounding operations. They are also needed for use cases that deal with numbers outside the DOUBLE range (approximately -10308 to 10308) or very close to zero (-10-308 to 10-308). For a general discussion of the limits of the DOUBLE type, see the Wikipedia article Double-precision floating-point format.