Floating point calculation problem in PHP

Source: Internet
Author: User
Tags square root

If you use PHP +-*/to calculate the floating-point number, you may encounter some errors in the calculation results, such as Echo Intval (0.58*100), will print 57, instead of 58, this is actually the bottom binary system of the computer can not accurately represent a floating point number of a bug, is a cross-language, I also encountered this problem with Python. So basically most of the language provides a precise calculation of the class library or function library, such as PHP has a BC high-accuracy function library, the following Dahne PHP trainer to introduce some of the commonly used BC high-precision function.

Example

The code is as follows
<?php $f = 0.58; Var_dump (Intval ($f * 100)); Why output 57?>

Why is the output 57? PHP bug?

I believe that a lot of students have had this kind of doubt, because the light asks me the similar question person to be many, not to mention bugs.php.net often people ask ...

To understand this, first we need to know the representation of floating-point numbers (IEEE 754):

Floating-point numbers, in the case of a 64-bit length (double), take the 1-bit sign bit (E), 11 exponent (Q), and 52-bit mantissa (M) (altogether 64 bits).

Sign bit: The highest bit represents the positive or negative of the data, 0 is a positive number, and 1 indicates a negative number.

Digits: Indicates that the data is a power of 2, and the exponent is represented by an offset code

Mantissa: A valid number that represents the decimal point of the data.

Here is the key point is that the decimal in the binary representation, about how to use binary decimal notation, you can Baidu, I do not repeat here, we key to understand, 0.58 for the binary representation, is an infinitely long value (the following number omitted the implied 1):

The binary representation of 0.58 is basically (52 bits) is: The binary representation of 00101000111101011100001010001111010111000010100011110.57 is basically (52 bits) is: 001000111101011100001010001111010111000010100011110 and the two binary, if only through the 52-bit calculation, respectively: www.111cn.net

0.58, 0.579999999999999960.57, 0.5699999999999999 as for the 0.58 * 100 specific floating-point multiplication, we do not consider so fine, interested can see (floating points), We are vague in mental arithmetic to see ... 0.58 * 100 = 57.999999999

Then you intval, Nature is 57 ....

Visible, the key point of this problem is: "You seem to have a poor decimal, in the computer binary representation is infinite"

So, do not think this is a PHP bug, this is the case .....

PHP floating point type in the +-*%/there is an inaccurate problem

For example:

1.

$a = 0.1;

$b = 0.7;

Var_dump (($a + $b) = = 0.8);

The printed value is Boolean false

Why is this? The PHP manual has the following warning message for floating-point numbers:

Warning

Floating point Accuracy

It is obvious that a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. 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.9999999999 ....

This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333. . .。

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 with any precision.

The code is as follows

<?php

$a = 0.1;
$b = 0.7;
Var_dump (Bcadd ($a, $b, 2) = = 0.8);

Bcadd-adds two high-precision numbers

Bccomp-compares two high-precision numbers, returns-1, 0, 1

bcdiv-divides two high-precision numbers

Bcmod-seeking high precision digital remainder

bcmul-multiply two high-precision numbers

bcpow-High Precision Digital exponentiation

Bcpowmod-to find high-precision digital exponentiation, number theory is very common

bcscale-Configure the default number of decimal places, equivalent to the "scale=" in the Linux BC

bcsqrt-to find the square root of high precision number

bcsub-to subtract two high-precision numbers

Sorted out some examples

PHP BC High-Accuracy library contains: Add, compare, divide, subtract, balance, multiply, n-squared, configure the default number of decimal points, squared. These functions are useful when it comes to money calculations, such as the price calculation of e-commerce.

The code is as follows
/**
* Two high-precision number comparisons
  * 
* @access Global
* @param float $left
* @param float $right
* @param int $scale The exact number of decimal places
  * 
* @return int $left = = $right return 0 | $left < $right return-1 | $left > $right return 1
  */
Var_dump (Bccomp ($left =4.45, $right =5.54, 2));
//-1
  
 /**
* Two high-precision numbers added
  * 
* @access Global
* @param float $left
* @param float $right
* @param int $scale The exact number of decimal places
  * 
* @return String
  */
Var_dump (Bcadd ($left =1.0321456, $right =0.0243456, 2));
//1.04
 
  /**
* Two high-precision number subtraction
  * 
* @access Global
* @param float $left
* @param float $right
* @param int $scale The exact number of decimal places
  * 
* @return String
  */
Var_dump (bcsub ($left =1.0321456, $right =3.0123456, 2));
//-1.98
  
 /**
* Divide two high-precision numbers
  * 
* @access Global
* @param float $left
* @param float $right
* @param int $scale The exact number of decimal places
  * 
* @return String
  */
Var_dump (Bcdiv ($left =6, $right =5, 2));
//1.20
 
 /**
* Multiply two high-precision numbers
  * 
* @access Global
* @param float $left
* @param float $right
* @param int $scale The exact number of decimal places
  * 
* @return String
  */
Var_dump (Bcmul ($left =3.1415926, $right =2.4569874566, 2));
//7.71
 
 /**
* Set the number of decimal places for BC functions
  * 
* @access Global
* @param int $scale The exact number of decimal places
  * 
* @return void
  */ 
Bcscale (3);
var_dump (' Bcdiv ', ' 6.55957 ');
//16.007

Note: In relation to the number of bits set, the excess is discarded rather than rounded.

Floating point calculation problem in PHP

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.