Php floating point comparison method details, php floating point details

Source: Internet
Author: User

Php floating point comparison method details, php floating point details

Floating Point Calculation Accuracy

First, let's look at an example:

<?php$a = 0.1;$b = 0.9;$c = 1;var_dump(($a+$b)==$c);var_dump(($c-$b)==$a);?>

$ 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:

<?php$a = 0.1;$b = 0.9;$c = 1;printf("%.20f", $a+$b); // 1.00000000000000000000printf("%.20f", $c-$b); // 0.09999999999999997780?>

$ 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

IntegerDivide by 2 to obtain the remainderMethod

Decimal partMultiply by 2 to get the integerMethod

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:

<?php$a = 0.1;$b = 0.9;$c = 1;var_dump(($c-$b)==$a);          // falsevar_dump(round(($c-$b),1)==round($a,1)); // true?>

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:

BcaddAdd two high-precision numbers

BccompReturns-1, 0, 1 after comparing two high-precision numbers.

BcdivDivision of two high-precision numbers

BcmodHigh-Precision Remainder

BcmulMultiply two high-precision numbers

BcpowHigh-Precision Digital Multiplication

BcpowmodEvaluate high-precision digital multiplication and square modulo

BcscaleConfigure the default number of decimal places, which is equivalent to "scale =" in Linux bc"

BcsqrtCalculate the square root of a precise number

BcsubSubtract two high-precision numbers

Example:

<?php$a = 0.1;$b = 0.9;$c = 1;var_dump(($c-$b)==$a);     // falsevar_dump(bcsub($c, $b, 1)==$a); // true?>

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.