PHP floating-point and precision issues

Source: Internet
Author: User

Floating point (also known as floating-point float, double-or real real) floating-point number representation:

Lnum [0-9]+
Dnum ([0-9]*[\.] {Lnum}) | ({lnum}[\.] [0-9]*]
Exponent_dnum [+]? ({lnum} | {Dnum}) [EE] [+-]? {Lnum})

On the accuracy of floating-point numbers

The precision of floating-point numbers is limited. Although depending on the system, PHP typically uses IEEE 754 double-precision format, the maximum relative error caused by rounding is 1.11e-16. A non-basic mathematical operation may give greater error and take into account the error transfer during the composite operation.
In addition, a rational number that can be accurately represented in decimal, such as 0.1 or 0.7, cannot be converted to binary format without losing a little bit of precision, regardless of how many of the mantissa can be accurately represented by the binary used internally. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9999999999999991118 ....
So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality.

NaN

Some mathematical operations produce a result that is represented by a constant NAN. This result represents a value that is undefined or not expressed in a floating-point operation. Any loose or strictly comparable result of this value to any other value is FALSE.

Since Nan represents any different value, it should not be compared with other values, including itself, and should be checked with Is_nan ().

Floating-point comparison problems

Never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality, but you can compare the number of floating-point numbers to be equal

1<?PHP2 if((0.01 + 0.003) = = 0.013)3     Echo' Equal ';4 Else5     Echo' Unequal ';6 //The result is unequal7 /**8 * Bccomp compares operands and right operands to return results in integer form9 * @param string $left _operand left operandTen * @param string $right _operand right operand One * @param int $scale to compare the number of digits after the decimal point by default of 2 A * @return int 0 means two numbers are equal, 1 means the left operand is large,-1 means the right operand is large -  **/ - $iResult=Bccomp((0.01 + 0.003), 0.013, 3);  the if(!$iResult) -     Echo' The operands is equal '; - ElseIf($iResult> 0) -     Echo' Left_operand is larger than the Right_operand '; + ElseIf($iResult< 0) -     Echo' Right_operand is larger than the Left_operand '; + Else A     Echo' Unknown result '; at //The operands is equal -?>

PHP floating-point and precision issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.