Php floating point comparison method

Source: Internet
Author: User
: This article mainly introduces the php floating point number comparison method. if you are interested in the PHP Tutorial, refer to it. Floating point calculation accuracy

First, let's look at an example:


  

$ A + $ B ==$ c returns true, correct
$ C-$ B == a returns false, error

Why?
After calculation, if the precision is 20 bits, the actual returned content is as follows:


  

$ C-$ B is 0.09999999999999997780, so false is returned for comparison with 0.1.

This problem occurs because the floating point calculation involves precision. when the floating point is converted to binary, the accuracy may be lost.

How to convert floating point to binary

The integer part is divided by 2 and the remainder is obtained.
The fractional part is rounded up by multiplying by 2.

For example, convert the number 8.5 to binary.
The integer is 8.
8/2 = 4 8% 2 = 0
4/2 = 2 4% 2 = 0
2/2 = 1 2% 2 = 0
1 is smaller than 2, so it does not need to be calculated. the binary value of integer 8 is 1000.

The decimal part is 0.5.
0.5x2 = 1.0
Since the decimal part after the integer is 0, you do not need to calculate it again.
The binary value of decimal number 0.5 is 0.1.

The binary value of 8.5 is 1000.1.

Calculate the binary value of the number 0.9
0.9x2 = 1.8
0.8x2 = 1.6
0.6x2 = 1.2
0.2x2 = 0.4
0.4x2 = 0.8
0.8x2 = 1.6
.... After that, the data is continuously iterated. when the truncation precision is N, the number after N will be removed, leading to loss of precision.
In the preceding example, the accuracy is lost when 0.9 is converted to binary, resulting in an error during comparison.

Therefore, never believe that the floating point number is accurate to the last digit, or compare whether the two floating point numbers are equal.

Correct method for comparing floating point numbers

1. use the round method for processing and then compare
Example:


  

2. use high-precision calculation methods
When performing the operation, use a high-precision calculation method to ensure that the accuracy is not lost.

The high-precision calculation method is as follows:
Bcadd adds two high-precision numbers
Bccomp compares two high-precision numbers and returns-1, 0, 1
Bcp separates two high-precision numbers
Bcmod for high-precision digital remainder
Bcmul multiply two high-precision numbers
Bcpow for high-precision digital multiplication
Bcpowmod evaluate high-precision digital multiplication and square modulo
The default number of decimal places in bcscale configuration is equivalent to "scale =" in Linux bc"
Bcsqrt: finding the square root of a precise number
Bcsub subtract two high-precision numbers

Example:


  

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the php floating point comparison method, including some content, hope to be helpful to friends who are interested in the PHP Tutorial.

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.