Home > Backend Development > PHP Tutorial > Detailed analysis of php floating point type

Detailed analysis of php floating point type

小云云
Release: 2023-03-20 21:36:02
Original
2276 people have browsed it

This article mainly shares with you a detailed analysis of PHP floating point types. I hope you will have a deeper understanding of PHP floating point types.

Floating point data has accuracy issues.

var_dump(0.9 ==(1-0.1)) ==>>  true;
var_dump(0.1 == (1-0.9)) ==>>  false;
Copy after login

Through the above example: It shows that floating point numbers should not be used to judge whether they are equal.

When you need to judge whether two floating point numbers are equal in PHP, you can use the bccomp function.
0 means equal 1 means greater than -1 means less than
bccomp (value 1, value 2, accurate to a few decimal places)

echo bccomp(0.1,(1-0.9),4) ==>>  0;echo bccomp(0.123,0.124,2) ==>>  0;echo bccomp(0.123,0.124,3) ==>>  -1;
Copy after login
Copy after login

float can display up to 14 digits after the decimal point. The capacities of double and float in PHP are the same.

Floating point data has accuracy issues.

var_dump(0.9 ==(1-0.1)) ==>>  true;
var_dump(0.1 == (1-0.9)) ==>>  false;
Copy after login

Through the above example: It shows that floating point numbers should not be used to judge whether they are equal.

When you need to judge whether two floating point numbers are equal in PHP, you can use the bccomp function.
0 means equal 1 means greater than -1 means less than
bccomp (value 1, value 2, accurate to several decimal places)

echo bccomp(0.1,(1-0.9),4) ==>>  0;echo bccomp(0.123,0.124,2) ==>>  0;echo bccomp(0.123,0.124,3) ==>>  -1;
Copy after login
Copy after login

float can display up to 14 digits after the decimal point. The capacities of double and float in PHP are the same.

Related recommendations:

php floating point type (Float)

The above is the detailed content of Detailed analysis of php floating point type. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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