Home  >  Article  >  Backend Development  >  What is the usage of floating point numbers in php

What is the usage of floating point numbers in php

藏色散人
藏色散人Original
2021-12-01 09:20:402089browse

php floating point number refers to the Float floating point type, which can be defined through syntax such as "$a = 1.234;$b = 1.2e3;$c = 7E-10;$d = 1_234.567;" Can.

What is the usage of floating point numbers in php

The operating environment of this article: windows7 system, PHP7.4 version, DELL G3 computer

What is the usage of PHP floating point numbers?

php Float floating point type

Floating point type (also called floating point number float, double precision number double or real number real) can be defined with any of the following syntax ; Precision in decimal digits (64-bit IEEE format).

Warning

Precision of floating point numbersFloating point numbers have limited precision. 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 arbitrary precision math functions or the gmp function.

Convert to floating point number

From strings

<?php
$a = 1.234; 
$b = 1.2e3; 
$c = 7E-10;
$d = 1_234.567; // 从 PHP 7.4.0 开始支持
?>
From other types

For other types of values, the The situation is similar to converting the value to int first and then to float. See the "Converting to Integer" section for more information.

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

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, also known as the machine epsilon or smallest unit integer, is the smallest difference value that can be accepted in the calculation.

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

If the string is numeric or leading numeric then it will resolve to the corresponding float value, otherwise it is converted to zero (0).

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. Any loose or strict comparison of this value with any other value (except true) will result in false. Since NAN represents any different value, NAN should not be compared with other values, including itself, and should be checked using is_nan().

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of What is the usage of floating point numbers 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