Javascript:比较两个浮点值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3343623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:15:20  来源:igfitidea点击:

Javascript: Comparing two float values

javascriptcomparisonfloating-pointnumbers

提问by Harold Sota

I have this JavaScript function:

我有这个 JavaScript 函数:

Contrl.prototype.EvaluateStatement = function(acVal, cfVal) {

    var cv = parseFloat(cfVal).toFixed(2);
    var av = parseFloat(acVal).toFixed(2);

   if( av < cv) // do some thing
}

When i compare float numbers av=7.00and cv=12.00the result of 7.00<12.00is false!

当我比较浮点数时av=7.00cv=12.00结果7.00<12.00false!

Any ideas why?

任何想法为什么?

回答by second

toFixed returns a string, and you are comparing the two resulting strings. Lexically, the 1 in 12 comes before the 7 so 12 < 7.

toFixed 返回一个字符串,您正在比较两个结果字符串。在词汇上,12 中的 1 出现在 7 之前,所以 12 < 7。

I guess you want to compare something like:

我猜您想比较以下内容:

(Math.round(parseFloat(acVal)*100)/100)

which rounds to two decimals

它四舍五入到两位小数

回答by Edward

Compare float numbers with precision:

精确比较浮点数:

var precision = 0.001;

if (Math.abs(n1 - n2) <= precision) {
  // equal
}
else {
  // not equal
}

UPD: Or, if one of the numbers is precise, compare precision with the relative error

UPD:或者,如果其中一个数字是精确的,则将精度与相对误差进行比较

var absoluteError = (Math.abs(nApprox - nExact)),
  relativeError = absoluteError / nExact;

return (relativeError <= precision);

回答by Alph.Dev

Comparing floats using short notation, also accepts floats as strings and integers:

使用短符号比较浮点数,也接受浮点数作为字符串和整数:

var floatOne = 2, floatTwo = '1.456';

Math.floor(floatOne*100) > Math.floor(floatTwo*100) 

(!) Note: Comparison happens using integers. What actually happens behind the scenes: 200 > 145

(!) 注意:比较使用整数进行。幕后实际发生的事情:200 > 145

Extend 100 with zero's for more decimal precision. For example use 1000 for 3 decimals precision.

用零扩展 100 以获得更高的小数精度。例如,使用 1000 表示 3 位小数精度。

Test:

测试:

var floatOne = 2, floatTwo = '1.456';
console.log(Math.floor(floatOne*100), '>', Math.floor(floatTwo*100), '=', Math.floor(floatOne*100) > Math.floor(floatTwo*100));

回答by George C.

The Math.fround() function returns the nearest 32-bit single precision float representation of a Number.

Math.fround() 函数返回最接近的 32 位单精度浮点数表示。

And therefore is one of the best choices to compare 2 floats.

因此是比较 2 个浮点数的最佳选择之一。

if (Math.fround(1.5) < Math.fround(1.6)) {
    console.log('yes')
} else {
    console.log('no')
}

>>> yes

// More examples:
console.log(Math.fround(0.9) < Math.fround(1));                            >>> true
console.log(Math.fround(1.5) < Math.fround(1.6));                          >>> true
console.log(Math.fround(0.005) < Math.fround(0.00006));                    >>> false
console.log(Math.fround(0.00000000009) < Math.fround(0.0000000000000009)); >>> false