Home >Backend Development >PHP Tutorial >PHP method example code to determine whether two floating point numbers are equal

PHP method example code to determine whether two floating point numbers are equal

怪我咯
怪我咯Original
2017-07-11 13:53:061793browse

Floating point numbers can be simply understood as decimals. Floating point numbers are digital representations of numbers belonging to a specific subset of rational numbers. They are used in computers to approximately represent any real number. Specifically, this real number is obtained by multiplying an integer or a fixed-point number (that is, the mantissa) by an integer power of a certain base (usually 2 in computers). This representation method is similar to the base 10 Scientific notation. This article mainly introduces the method of php to determine whether two floating point numbers are equal. It involves the skills of operating floating point numbers in PHP. It is more practical. Friends who need it can refer to it

Since it is not completely correct to directly use == to determine whether floating point numbers are equal, a method is given below. First set a precision. If they are equal within the precision range, they are considered equal, otherwise Think it can't

<?php
$delta = 0.00001;
$a = 1.00000001;
$b = 1.00000000;
if (abs($a - $b) < $delta) { /* $a and $b are equal */ }
?>

The above is the detailed content of PHP method example code to determine whether two floating point numbers are 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