Home  >  Article  >  Backend Development  >  PHP floating point decimal comparison: why not equal?

PHP floating point decimal comparison: why not equal?

WBOY
WBOYOriginal
2024-03-08 16:24:041165browse

PHP floating point decimal comparison: why not equal?

PHP Floating Point Decimal Comparison: Why not equal?

In programming, we often encounter situations where we need to compare whether two floating point decimals are equal. However, when we use PHP to compare floating point decimals, we often find that two seemingly equal floating point numbers are not equal due to precision issues. This situation is often confusing. This article will delve into the issue of floating point number comparison in PHP and give specific code examples to illustrate why unequal situations occur when floating point numbers are compared.

In computers, floating-point numbers are represented in binary. Since binary cannot accurately represent certain decimal decimals (such as 0.1), floating-point numbers in computers are often approximate values. This leads to accuracy problems when comparing floating point numbers, and situations such as 0.1 0.2 != 0.3 often occur.

Let us illustrate this problem through a specific example. Suppose we have two floating point numbers 0.1 and 0.2, and we try to use PHP to compare whether their sum is equal to 0.3:

$num1 = 0.1;
$num2 = 0.2;

$sum = $num1 + $num2;
$expected = 0.3;

if ($sum == $expected) {
    echo "相等";
} else {
    echo "不相等";
}

When we run the above code, it is likely that the output result will be "not equal" , this is because 0.1, 0.2 and 0.3 are not exact values ​​in the computer, but approximate values, so there will be precision errors when making comparisons.

To solve this problem, we can use the built-in function round() in PHP to round floating point numbers to avoid precision errors. Modify the above code as follows:

$num1 = 0.1;
$num2 = 0.2;

$sum = $num1 + $num2;
$expected = 0.3;

if (round($sum, 10) == round($expected, 10)) {
    echo "相等";
} else {
    echo "不相等";
}

By using the round() function, we retain the number of digits after the decimal point in the floating point number to make the calculation result more accurate. In this code example, we use round($sum, 10) and round($expected, 10) to compare the sum of two floating point numbers to the accuracy of the expected value, thus Avoid inequalities caused by precision errors.

In summary, the problem of unequal comparison of floating point numbers in PHP is mainly caused by the approximation and precision errors of floating point numbers. When comparing floating point numbers, we should pay attention to the issue of accuracy and try to avoid using the equal sign directly to compare floating point numbers. By handling precision in a suitable way, we can ensure the accuracy of floating point comparisons, thereby improving the stability and reliability of our programs.

I hope that through the introduction of this article, readers can have a deeper understanding of the floating point number comparison problem in PHP and avoid bugs caused by precision errors in actual development.

The above is the detailed content of PHP floating point decimal comparison: why not equal?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn