添加两个小数 Hive SQL
Adding two decimal numbers Hive SQL
我正在尝试使用 Hive SQL 将两个数字相加。
select 4.6 + 3.1 from <table> => 7.699999999999999
但是,
select 4.7 +3.2 from <table> => 7.9
我知道结果的 舍入 会给出预期的输出,但问题是为什么首先会出现这种不同的行为?
我在 Apache spark 1.6.2 中使用 hive 2.11(hive 上下文)。
您所观察到的不是特定于 Hive 的。示例:
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
>>> 4.7+3.2
7.9
>>> 4.6+3.1
7.699999999999999
这与某些语言中双精度数在内存中的存储方式有关(浮点数):
https://en.wikipedia.org/wiki/Floating_point#Internal_representation
另请参阅:
- Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
- What Every Computer Scientist Should Know About Floating-Point Arithmetic
我正在尝试使用 Hive SQL 将两个数字相加。
select 4.6 + 3.1 from <table> => 7.699999999999999
但是,
select 4.7 +3.2 from <table> => 7.9
我知道结果的 舍入 会给出预期的输出,但问题是为什么首先会出现这种不同的行为?
我在 Apache spark 1.6.2 中使用 hive 2.11(hive 上下文)。
您所观察到的不是特定于 Hive 的。示例:
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
>>> 4.7+3.2
7.9
>>> 4.6+3.1
7.699999999999999
这与某些语言中双精度数在内存中的存储方式有关(浮点数): https://en.wikipedia.org/wiki/Floating_point#Internal_representation
另请参阅:
- Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
- What Every Computer Scientist Should Know About Floating-Point Arithmetic