Home >Backend Development >PHP Tutorial >Parsing PHP data type floating point type (Float)

Parsing PHP data type floating point type (Float)

怪我咯
怪我咯Original
2017-04-05 10:42:032547browse

Floating point type (also called floating point numberfloat, double precision number double or real number real) can be defined with any of the following syntax:

<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

Floating point number representation:

LNUM [0-9]+
DNUM ([0-9]*[\.]{LNUM} ) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})

The word length of floating point numbers is platform-dependent, although typically the maximum value is 1.8e308 with a precision of 14 decimal digits (64-bit IEEE format).

Warning

The precision of floating point numbers

The precision of floating point numbers is limited. Although it depends on the system, PHP usually uses the IEEE 754 double format, so the maximum relative error due to rounding is 1.11e-16. Non-basic mathematical operations may give larger errors, and error propagation when doing compound operations needs to be taken into account.

In addition, rational numbers that can be accurately represented in decimal, such as 0.1 or 0.7, no matter how many mantissas there are, cannot be accurately represented by the binary used internally, and therefore cannot be converted to binary without losing a little precision. Format. This can lead to 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 believe that the 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 mathfunction or the gmp function.

See » The Floating Point Guide web page for a simple explanation.

Convert to floating point number

If you want information about when and how to convert a string to a floating point number, see "Converting a string to a numeric value" Festival. For other types of values, the situation is similar to converting the value to an integer and then to a floating point. See the "Converting to Integer" section for more information. As of PHP 5, if you try to convert an object to a floating point number, an E_NOTICE error message is issued.

Compare floating point numbers

As the above warning message says, due to internal expression reasons, there is a problem in comparing two floating point numbers for equality. However, there are roundabout ways to compare floating point values.

To test floating-point numbers for equality, use a minimum error value that is only a tiny bit larger than that value. This value is also called the machine minimum value (epsilon) or the smallest unit is an integer , which is the smallest difference value acceptable in the calculation.

$a and $b are equal to five decimal places of precision.

<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if(abs($a-$b) < $epsilon) {
   echo "true";
}
?>

NaN

Certain mathematical operations produce a result represented by the

constant NAN. This result represents a value that is undefined or unrepresentable in floating-point arithmetic. The result of any loose or strict comparison of this value with any other value is FALSE.

Since NAN represents any different value, NAN should not be compared with other values, including itself, and should be checked using is_nan().

The above is the detailed content of Parsing PHP data type floating point type (Float). 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