HIVE 长十进制结果

HIVE long decimal results

在配置单元中,当执行以下 select 语句时,它返回 1.20000000000000002 作为结果而不是 1.2。

select 1.1 + 0.1;

实际上,当存储类似的 FLOAT 值时,它在我们的配置单元表中做同样的事情,所以我想问问这个。 谢谢

这就是浮点运算的工作原理。如果您需要精度,cast 结果为 decimal.

select cast(1.1+0.1 as decimal(9,1))

From Hive documentation

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.