Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,哪种比较两个带n个小数的浮点的方法最有效?_Php_Performance_Floating Point - Fatal编程技术网

在PHP中,哪种比较两个带n个小数的浮点的方法最有效?

在PHP中,哪种比较两个带n个小数的浮点的方法最有效?,php,performance,floating-point,Php,Performance,Floating Point,众所周知,比较浮点与比较整数或字符串有点不同,有许多帖子解释了这一点,例如: 等等 我的问题是——如果我需要比较精度为n位小数(用户定义精度)的两个浮点,哪种“合法”比较方法是最好的方法?让我们知道是否存在更有效的方法。到目前为止,我有几个例子。以下示例包含2位小数,但我确实关心实数中的n位小数: $x = anyFloat.variableValue; $y = 0.00; // known number // 1 round($x, 2) === round($y, 2) //

众所周知,比较浮点与比较整数或字符串有点不同,有许多帖子解释了这一点,例如:

  • 等等
我的问题是——如果我需要比较精度为n位小数(用户定义精度)的两个浮点,哪种“合法”比较方法是最好的方法?让我们知道是否存在更有效的方法。到目前为止,我有几个例子。以下示例包含2位小数,但我确实关心实数中的n位小数:

$x = anyFloat.variableValue;
$y = 0.00; // known number

// 1
round($x, 2) === round($y, 2)

// 2
round($x, 2) === 0.00

// 3
number_format($x, 2) === number_format(0.00, 2)

// 4
abs($a-$b) < PHP_FLOAT_EPSILON

// 5
abs(round($x,2)-0.00) < PHP_FLOAT_EPSILON

// 6
any other?
$x=anyFloat.variableValue;
$y=0.00;//已知数
// 1
圆形($x,2)==圆形($y,2)
// 2
四舍五入($x,2)==0.00
// 3
数字格式($x,2)==数字格式(0.00,2)
// 4
abs($a-$b)
这些都不正确。任何解决方案都是特定于应用程序的。如果要进行任何可以容忍以前错误的比较,则必须具有有关已发生错误的信息以及其他信息,并且这些信息必须是问题陈述的一部分。