A FAQ for PHP floating-point numbers

Source: Internet
Author: User

About the floating-point number of PHP, I've written an article before: What you should know about PHP floating-point numbers (all ' bogus ' on the float in PHP)

However, I missed a point, which is the answer to the following FAQ:

    1. $f = 0.58;
    2. Var_dump (Intval ($f * 100)); Why output 57
    3. ?>

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

    1. The binary representation of 0.58 is basically (52 bits) is: 0010100011110101110000101000111101011100001010001111
    2. The binary representation of 0.57 is basically (52 bits) is: 001000111101011100001010001111010111000010100011110

and the binary of both, if only through the 52-bit calculation, respectively:

    1. 0.58-0.57999999999999996
    2. 0.57-0.5699999999999999

As for the 0.58 * 100 of the specific floating-point multiplication, we do not consider so thin, interesting to see (floating point), 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 .....

http://bbs.55bbs.com/thread-9954608-1-1.html http://bbs.55bbs.com/thread-9954607-1-1.html http://www.xici.net/ d229677444.htm http://www.xici.net/d229677616.htm http://www.xici.net/d229677676.htm http://www.xici.net/ D229677752.htm http://www.xici.net/d229677802.htm http://www.xici.net/d229677860.htm

A FAQ for PHP floating-point numbers

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.