Home  >  Article  >  Backend Development  >  Detailed analysis of php floating point type

Detailed analysis of php floating point type

小云云
小云云Original
2018-03-08 10:53:572224browse

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;

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;

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;

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;

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!

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