Home  >  Article  >  Backend Development  >  What is the difference between float and double in php

What is the difference between float and double in php

WBOY
WBOYOriginal
2022-02-17 15:50:092444browse

In PHP, there is no difference between float and double. Floating point type is also called floating point number float, double precision number double or real number real in PHP. Float, double or real are the same data type and can be defined using the same syntax.

What is the difference between float and double in php

The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

What is the difference between float and double in php

In order to distinguish the difference between float and double in php, I use the following code to output them respectively.

$vad = 1.1;
print gettype($vad);
var_dump($vad);

This will output:

double
float(1.1)

It looks different, but in fact in php, there is no difference. float, double or real are the same data type. Float (also called float, double or real. At the C level, everything is stored as a double.

The actual size is still platform dependent. See the manual for details:

http://www.php.net/manual/en/language.types.float.php

The precision of floating point numbers

The precision of floating point numbers is limited. Although Depending on the system, PHP usually uses the IEEE 754 double format, then the maximum relative error due to rounding is 1.11e-16. Non-basic math operations may give larger errors, and take into account when doing compound operations Error propagation.

Additionally, rational numbers that can be represented exactly in decimal, such as 0.1 or 0.7, no matter how many mantissas there are, cannot be represented exactly by the binary used internally, and therefore cannot be converted without losing a little precision. is in binary format. This can cause confusing results: for example, floor((0.1 0.7)*10) will usually return 7 instead of the expected 8, because the internal representation of the result is actually something like 7.9999999999999991118….

So never trust that a floating point number result is accurate to the last digit, and never compare whether two floating point numbers are equal. If you really need higher precision, you should use the arbitrary precision math function or the gmp function.

Recommended learning: "PHP video tutorial"

The above is the detailed content of What is the difference between float and double in php. 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