PHP floating point number FAQ

Source: Internet
Author: User
I have written an article about the floating point number in PHP: you should know about the PHP floating point number (All 'bogg' aboutthefloatinPHP). However, I missed a point at the time, that is, to answer the following Frequently Asked Questions: & lt ;? Php

I have written an article about PHP floating point numbers: you should know about PHP floating point numbers (All 'bogg' about the float in PHP)

However, I did not answer the following Frequently Asked Questions:

 
 
  1. $ F = 0.58;
  2. Var_dump (intval ($ f * 100); // for output 57
  3. ?>

Why is output 57? PHP bug?

I believe many people have such questions, because there are many people who ask similar questions, not to mention bugs.php.net...

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

Floating point number. taking the 64-bit length (double precision) as an example, one-bit sign bit (E), 11 index bit (Q), and 52-bit ending number (M) are used) (64-bit in total ).

Symbol bit: the highest bit indicates positive and negative data, 0 indicates positive data, and 1 indicates negative data.

Exponent bit: the power of data at the bottom of 2, and the exponent is represented by an offset code.

Ending number: a valid number after the decimal point of the data.

The key point here is that decimal places are represented in binary. you can refer to Baidu for how decimal places are represented in binary. I will not describe them here. we need to understand the key points, 0.58 for binary representation, it is an infinitely long value (the following number saves the implicit 1 )..

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

The binary values of the two are:

 
 
  1. 0.58 -> 0.57999999999999996
  2. 0.57 -> 0.5699999999999999

As for the specific Floating-point multiplication of 0.58*100, we will not consider that detail. if you are interested, you can see the Floating point. we will look at it in a fuzzy way... 0.58*100 = 57.999999999

Then you intval It. Naturally, it's 57 ....

It can be seen that the key point of this problem is: "You seem to have a poor decimal number, but it is infinite in the binary representation of the computer"

So, no longer think this is a PHP bug. this is the case .....

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.