为什么2个parseFloat变量的总和给我一个不正确的十进制数 [英] Why does the sum of 2 parseFloat variables give me an incorrect decimal number

查看:126
本文介绍了为什么2个parseFloat变量的总和给我一个不正确的十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个小功能:

 < script type =text / javascript> 

函数printFloat(){
var myFloatNumber1 = document.getElementById('floatNumber1');
var myFloatNumber2 = document.getElementById('floatNumber2');
alert(parseFloat(myFloatNumber1.value)+ parseFloat(myFloatNumber2.value))
}

< / script>

< input type =textid =floatNumber1>< / input>
< input type =textid =floatNumber2>< / input>

< input type =buttononclick =printFloat()/>

在字段1中输入:字段2中的221.58
我输入:2497.74



我希望输入字段中的2个数字总和为2位数字:2719.32
但结果是不正确的数字...:2719.3199999999997



一轮会做这项工作,但我只是不明白为什么代码会在这个数字上做到这一点......
在其他数字组合上,总和是正确的...

解决方案

From 浮点指南:


为什么我的数字不像0.1 + 0.2加起来不错的一轮0.3,而
,而我得到一个奇怪的结果,如
0.30000000000000004?



因为内部,计算机使用
格式(二进制浮点),
不能准确地表示数字
,比如0.1,0.2或0.3。

<当代码被编译时r
解释,您的0.1已经是
,以
格式四舍五入到最接近的数字,即使在
计算发生之前也会导致小的
舍入误差。

在您的情况下,当您输入的值由 parseFloat()


为什么像0.1 + 0.4这样的其他计算能够正常工作?



在这种情况下,结果(0.5)可以是
,恰好可以表示为
浮点数,并且
可能用于舍入误差在
输入数字中可以相互抵消
- 但这不一定是可靠的(例如,当这两个数字首先存储在不同大小的浮动
点表示中时,
舍入误差可能不会抵消每个
其他数据)。

在其他情况下,如0.1 + 0.3,
结果实际上并不是0.4,但
足够接近,0.4是最接近结果的最短
数值
比任何其他浮点
编号。然后许多语言显示
这个数字,而不是将
的实际结果转换回最接近的
小数。



If I have this little function:

<script type="text/javascript">

 function printFloat(){
      var myFloatNumber1 = document.getElementById('floatNumber1');
      var myFloatNumber2 = document.getElementById('floatNumber2');
      alert(parseFloat(myFloatNumber1.value) + parseFloat(myFloatNumber2.value))
 }

</script>

<input type="text" id="floatNumber1"></input>
<input type="text" id="floatNumber2"></input>

<input type="button" onclick="printFloat()"/>

in field 1 I enter: 221.58 in field 2 I enter: 2497.74

I expect the sum of 2 numbers in the input fields to be a 2 number digit: 2719.32 But the result is a incorrect number... : 2719.3199999999997

a round would do the job, but I just don't get why the code does that on this number... On other number combinations, the sum is correct...

解决方案

From The Floating-Point-Guide:

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your "0.1" is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

In your case, the rounding errors happen when the values you entered are converted by parseFloat().

Why do other calculations like 0.1 + 0.4 work correctly?

In that case, the result (0.5) can be represented exactly as a floating-point number, and it’s possible for rounding errors in the input numbers to cancel each other out - But that can’t necessarily be relied upon (e.g. when those two numbers were stored in differently sized floating point representations first, the rounding errors might not offset each other).

In other cases like 0.1 + 0.3, the result actually isn’t really 0.4, but close enough that 0.4 is the shortest number that is closer to the result than to any other floating-point number. Many languages then display that number instead of converting the actual result back to the closest decimal fraction.

这篇关于为什么2个parseFloat变量的总和给我一个不正确的十进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆