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

怪我咯
Release: 2023-03-13 14:38:01
Original
1746 people have browsed it

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 */ }
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!