Javascript Math Error:Inexact Floats [英] Javascript Math Error: Inexact Floats

查看:89
本文介绍了Javascript Math Error:Inexact Floats的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

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一个因子(在这种情况下为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

我想如果可以做一个preg_match对于额外的000s然后相应地调整因子,可以避免浮动错误。
然而,最终的解决方案是数学库

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 10 de

现在你仍然可以记住只有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 10 0 = 123.0

123 x 10 3 = 123,000.0

123 x 10 6 = 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 被称为 有效数字 ,又名系数/尾数 de exponent ,又名 scale / characteristics 。像往常一样,计算机使用基数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.

  • What Every Computer Scientist Should Know About Floating-Point Arithmetic
  • Wikipedia/IEEE 754

这篇关于Javascript Math Error:Inexact Floats的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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