数学不准确的Javascript:安全使用JS的重要的东西? [英] Math inaccuracy in Javascript: safe to use JS for important stuff?

查看:240
本文介绍了数学不准确的Javascript:安全使用JS的重要的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很无聊,所以我开始在控制台里四处乱逛,偶然发现了这个(忽略语法错误):
某些变量test有一个值,我乘以10K,它突然变成不同的数字(你可以把它称为舍入误差,但这取决于你需要多少准确度)。然后我把这个数字乘以10,然后又变回来。



这对我提出了一些问题:


  • 这已经确定了吗?即一个可以考虑的数字?

  • 有没有办法解决这个问题?即(在数据类型的限制范围内)以Javascript完成数学计算。

  • 第二次操作后改变的数字应该被解释为改回原始数字或改变再次,因为不准确'?$ / b>


我不确定这应该是一个单独的问题,但我实际上是试图在小数点后一定数量的整数。我研究了一下,发现了两种方法:
$ b

 > 方法A

 函数roundNumber(数字,数字){
var multiple = Math.pow(10,digits);
返回Math.floor(number * multiple)/ multiple;

code

$ b b方法B



$ $ p $ code $函数roundNumber(数字,数字){
返回Number(number.toFixed(digits));





直觉上我更喜欢B方法高效),但我不知道幕后发生了什么,所以我不能真正判断。任何人都有这个想法?或者一个基准的方法呢?为什么没有本地的round_to_this_many_decimals函数? (一个返回一个整数,而不是一个字符串)

解决方案


/ b>

Javascript使用标准的双精度浮点数,所以精度限制与使用它们的任何其他语言语言。这是处理器用来处理浮点数的原生格式。


有没有办法解决这个问题?即完全准确地(在数据类型的限制内)在Javascript中进行数学运算。

没有。精度限制在于数字的存储方式。浮点数没有完全的准确性,所以不管你如何进行计算,你都不能达到绝对的精度,因为结果会返回到浮点数。



如果您想要完整的准确性,那么您需要使用不同的数据类型。

lockquote

如果第二次操作后更改的数字被解释为
'换回原来的数字'或'再次改变,因为
的不准确'?

再次改变。

当一个数字被转换为文本来显示时,它被舍入到一定数量的数字。看起来像他们确切的数字不是,它只是在精度的限制不显示出来。



当数字改回,这只是因为舍入再次隐藏了精度的限制。每个计算都会增加或减少数字中的一个小错误,有时恰好会使数字接近原来的数字。尽管它看起来更准确,但实际上不太准确,因为每个计算都会增加一些不确定性。

I was bored, so I started fidlling around in the console, and stumbled onto this (ignore the syntax error):

Some variable "test" has a value, which I multiply by 10K, it suddenly changes into different number (you could call it a rounding error, but that depends on how much accuracy you need). I then multiply that number by 10, and it changes back/again.

That raises a few questions for me:

  • How in accurate is Javascript? Has this been determined? I.e. a number that can be taken into account?
  • Is there a way to fix this? I.e. to do math in Javascript with complete accuracy (within the limitations of its datatype).
  • Should the changed number after the second operation be interpreted as 'changing back to the original number' or 'changing again, because of the inaccuracy'?

I'm not sure whether this should be a separate question, but I was actually trying to round numbers to a certain amount after the decimal point. I've researched it a bit, and have found two methods:

 > Method A

function roundNumber(number, digits) {
    var multiple = Math.pow(10, digits);
    return Math.floor(number * multiple) / multiple;
}

 > Method B

function roundNumber(number, digits) {
    return Number(number.toFixed(digits));
}


Intuitively I like method B more (looks more efficient), but I don't know what going on behind the scenes so I can't really judge. Anyone have an idea on that? Or a way to benchmark this? And why is there no native round_to_this_many_decimals function? (one that returns an integer, not a string)

解决方案

How in accurate is Javascript?

Javascript uses standard double precision floating point numbers, so the precision limitations are the same as for any other language that uses them, which is most languages. It's the native format used by the processor to handle floating point numbers.

Is there a way to fix this? I.e. to do math in Javascript with complete accuracy (within the limitations of its datatype).

No. The precision limitations lies in the way that the number is stored. Floating point numbers doesn't have complete accuracy, so no matter how you do the calculations you can't achieve absolute accuracy as the result goes back into a floating point number.

If you want complete accuracy then you need to use a different data type.

Should the changed number after the second operation be interpreted as 'changing back to the original number' or 'changing again, because of the inaccuracy'?

It's changing again.

When a number is converted to text to be displayed, it's rounded to a certain number of digits. The numbers that look like they are exact aren't, it's just that the limitations in precision doesn't show up.

When the number "changes back" it's just because the rounding again hides the limitations in the precision. Each calculation adds or subtracts a small inaccuracy in the number, and sometimes it just happens to take the number closer to the number that you had originally. Eventhough it looks like it's more accurate, it's actually less accurate as each calculation adds a bit of uncertainty.

这篇关于数学不准确的Javascript:安全使用JS的重要的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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