Floating-point type (float) parsing PHP data type

Source: Internet
Author: User

Floating-point (also known as floating-point float, double-or real-precision) can be defined with any of the following syntax:

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

The form of a floating-point number indicates:

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

The length of the floating-point number is relative to the platform, although the usual maximum value is 1.8e308 and has a precision of 14-bit decimal digits (64-bit IEEE format).

Warning

The precision 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. If you do need a higher precision, you should use any mathematical or GMP function of any precision.

See the» Floating point Guide 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 the "string conversion to numeric Value" section. For other types of values, the case is similar to converting a value to an integer before converting it to a floating-point. See the "Convert to Integer" section for more information. From PHP 5, if you attempt to convert an object to a floating-point number, a e_notice error message is emitted.

Compare floating point numbers

As the warning message says, it is problematic to compare two floating-point numbers for reasons of internal expression. There are, however, circuitous ways to compare floating-point values.

To test whether a floating-point number is equal, use a minimum error value that is only a little larger than the value. This value is also known as the Machine Minimum (epsilon) or the smallest element to take an integer, which is the smallest differential value that can be accepted in the calculation.

$a and $b are equal within five-bit precision after the decimal point.

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

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 ().

Related Article

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.