PHP questions about the amount comparison

Source: Internet
Author: User

When doing e-commerce generally will involve the amount of comparison, according to the normal way of thinking with ><= these symbols can be. But it's going to be a big deal when it comes to the program. Now take a look at this piece of code:

$f = 0.07;var_dump ($f * 100 = = 7);//Output False

The output will be unexpected, output false, why is this? Actually, this is related to the principle of storing decimals in a computer. We all know that computers can only store 0 and 1, we are accustomed to using 10 of the data in daily life, such as the number of 0.07 is stored in the computer will have a loss of precision, so that the calculated results will be biased.

So how to solve this problem? Although the computer stores decimal points, but the deviation is very small, as in the above example 0.07 * 100 if the 20 digits after the decimal point is displayed, the final value is as follows

$f = 0.07; //输出7.00000000000000088818 echo number_format( $f * 100, 20)You can see that it has been over 10 decimal points. In practice we usually do not need to be accurate to the back so many digits, in terms of the amount is usually accurate to the back of the 3-bit good. 0.07*100 and 7 will be equal if they are exactly three digits behind the decimal point. A Bccomp function is provided in PHP to handle this comparison. $f = 0.07; var_dump( $f * 100 == 7); //输出0,表示两个数字精度为小数点后3位的时候相等 var_dump( bccomp ( $f * 100, 7, 3));Although finally solved the problem, but still want to understand why 0.07 such floating-point number will have the precision loss, after a period of research, found that the cause of the error: The floating-point numbers in the decimal place when converted to binary. The fractional part of a floating-point number is converted to a binary rule: multiply by 2, that is, each step multiplies the decimal fraction by 2, and the numbers (0 or 1) to the left of the decimal point of the product are used as numbers in the binary notation until the accuracy is met. It is calculated that 0.07 is still not finished even after 60 digits have been calculated. In a computer, a 32-bit computer has a floating-point count of 23 bits and 64 bits of 52 bits. So the extra parts will be discarded. The tests were performed on 64-bit machines. $bin "" ; $int = 7; $base = 100; echo "<table border=‘1‘>" ; echo "<td width=‘50‘>位数</td>" ; echo "<td width=‘50‘>x2</td>" ; echo "<td width=‘50‘>位值</td>" ; for ( $i = 0;  $i <= 60;  $i ++) {      echo "<tr>" ;      echo "<td>$i</td>" ;      $int $int * 2;      echo "<td>$int</td>" ;      if ( $int == 100) {          $bin .= "1" ;          echo "<td>1</td>" ;          break ;      }      if ( $int > 100) {          $bin .= "1" ;          $int $int $base ;          echo "<td>1</td>" ;      else {          $bin .=  "0" ;          echo "<td>0</td>" ;      }      echo "</td>" ;      echo "</tr>" ; } echo "</table>" ; echo $bin ;To reverse the binary of the previous example conversion: /*    输出内容    0.070000000000000006661338147751    0.070000000000000006661338147751   */ $f = 0.0; $bin "0001000111101011100001010001111010111000010100011110101110000" ; $l strlen ( $bin ); for ( $i = 0;  $i $l $i ++) {      if ( $bin [ $i ] > 0) {          $f $f + pow(2, -( $i + 1));      } } echo number_format( $f , 30); $f = 0.07; echo "<br />" ; echo number_format( $f , 30);

PHP Questions about amount comparison (RPM)

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.