Javascript 数学错误:不精确的浮点数 [英] Javascript Math Error: Inexact Floats

查看:27
本文介绍了Javascript 数学错误:不精确的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
JavaScript 的数学有问题吗?
如何存储浮点数?什么时候重要?

代码:

var tax= 14900*(0.108);
alert(tax);

以上给出了1609.2的答案

The above gives an answer of 1609.2

var tax1= 14900*(10.8/100);
alert(tax1);

以上给出了1609.200000000003的答案

The above gives an answer of 1609.200000000003

为什么?我想我可以四舍五入这些值,但为什么会发生这种情况?

why? i guess i can round up the values, but why is this happening?

更新:找到了解决问题的临时解决方案.

UPDATE: Found a temp solution for the problem.

先乘:

(14900*10.8)/100 = 1609.2

不过

(14898*10.8)/100 = 1608.9840000000002

为此,将 10.8 乘以 factor(本例中为 100) 并调整分母:

For this multiply the 10.8 by a factor(100 in this case) and adjust the denominator:

(14898*(10.8*100))/10000 = 1608.984

我想如果可以对额外的 000 进行 preg_match,然后相应地调整 factor,则可以避免浮动错误.然而,最终的解决方案是数学库.

I guess if one can do a preg_match for the extra 000s and then adjust the factor accordingly, the float error can be avoided. The final solution would however be a math library.

推荐答案

浮点值为不准确.

这几乎就是问题的答案.存在有限精度,这意味着某些数字无法准确表示.

This is pretty much the answer to the question. There is finite precision, which means that some numbers can not be represented exactly.

某些语言在语言级别支持任意精度的数字类型/有理数/复数等,但不支持 Javascript.C 和 Java 都没有.

Some languages support arbitrary precision numeric types/rational/complex numbers at the language level, etc, but not Javascript. Neither does C nor Java.

IEEE 754 标准浮点值不能表示例如0.1 完全正确.这就是为什么必须非常小心地使用美分等进行数值计算.有时,解决方案是将美分值存储为整数,而不是将美元存储为浮点值.

The IEEE 754 standard floating point value can not represent e.g. 0.1 exactly. This is why numerical calculations with cents etc must be done very carefully. Sometimes the solution is to store values in cents as integers instead of in dollars as floating point values.

要了解浮点值为何不精确,请考虑以下模拟:

To see why floating point values are imprecise, consider the following analog:

  • 你的记忆力只能记住 5 位数字
  • 您希望能够在尽可能广泛的范围内表示值

在表示整数时,可以表示-99999+99999范围内的值.超出这些范围的值需要您记住 5 位以上的数字,而这(就本示例而言)您无法做到.

In representing integers, you can represent values in the range of -99999 to +99999. Values outside of those range would require you to remember more than 5 digits, which (for the sake of this example) you can't do.

现在你可以考虑一个定点表示,比如abc.de.现在您可以表示 -999.99+999.99 范围内的值,最多 2 位精度,例如3.14-456.78

Now you may consider a fixed-point representation, something like abc.de. Now you can represent values in the range of -999.99 to +999.99, up to 2 digits of precision, e.g. 3.14, -456.78, etc.

现在考虑一个浮点​​版本.凭借您的足智多谋,您想出了以下方案:

Now consider a floating point version. In your resourcefulness, you came up with the following scheme:

n = abc x 10de

现在你仍然只能记住 5 个数字 a, b, c, d, e,但您现在可以表示更广泛的数字,甚至是非整数.例如:

Now you can still remember only 5 digits a, b, c, d, e, but you can now represent much wider range of numbers, even non-integers. For example:

123 x 100 = 123.0

123 x 103 = 123,000.0

123 x 106 = 123,000,000.0

123 x 10-3 = 0.123

123 x 10-6 = 0.000123

这就是名称浮点"的含义.应运而生:小数点四处飘"在上面的例子中.

This is how the name "floating point" came into being: the decimal point "floats around" in the above examples.

现在您可以表示范围很广的数字,但请注意,您无法表示 0.1234.你也不能代表123,001.0.事实上,有很多价值是你无法代表的.

Now you can represent a wide range of numbers, but note that you can't represent 0.1234. Neither can you represent 123,001.0. In fact, there's a lot of values that you can't represent.

这就是浮点值不精确的原因.它们可以表示范围很广的值,但由于您只能使用固定数量的内存,因此您必须牺牲精度以换取量级.

This is pretty much why floating point values are inexact. They can represent a wide range of values, but since you are limited to a fixed amount of memory, you must sacrifice precision for magnitude.

abc 被称为 significand,又名系数/尾数.de指数,也就是比例/特征.像往常一样,计算机使用基数 2 而不是 10.(位,真的),它还必须记住有效数和指数的符号.

The abc is called the significand, aka coefficient/mantissa. The de is the exponent, aka scale/characteristics. As usual, the computer uses base 2 instead 10. In addition to remembering the "digits" (bits, really), it must also remember the signs of the significand and exponent.

单精度浮点类型通常使用 32 位.双精度通常使用 64 位.

A single precision floating point type usually uses 32 bits. A double precision usually uses 64 bits.

这篇关于Javascript 数学错误:不精确的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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