PHP floating point number accuracy problem summary _ PHP Tutorial

Source: Internet
Author: User
PHP floating point number accuracy problem summary. PHP floating point number precision problem summary this article mainly introduces the PHP floating point number precision problem summary, this article focuses on the PHP floating point number precision loss problem, with three paragraphs different ways to explain the PHP floating point precision problem summary

This article mainly introduces the summary of the floating point number precision problem in PHP. This article focuses on the loss of the floating point precision in PHP, and explains the cause and solution of this problem in three different paragraphs, for more information, see

I. precision loss of PHP floating point numbers

Let's take a look at the following code:

The code is as follows:

$ F = 0.57;

Echo intval ($ f * 100); // 56

The result may be a little unexpected. PHP follows IEEE 754 dual precision:

Floating point number, expressed in 64-bit dual precision, with one-bit sign bit (E), 11 exponent bit (Q), and 52-bit ending number (M) (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.

Let's take a look at how decimal points are represented in binary:

Multiply the decimal part by 2, then take the integer part, multiply the remaining decimal part by 2, and then take the integer part, and multiply the remaining decimal part by 2, the fractional part is always obtained, but the decimal part like 0.57 is always multiplied, and the decimal part cannot be 0. the decimals of valid bits are expressed in binary format but are infinite.

The binary representation of 0.57 is basically (52 bits): 0010001111010111000010100011110101110000101000111101

If there are only 52 digits, 0.57 = "0.56999999999999995

It's not hard to see the unexpected result above.

II. precision of PHP floating point numbers

First look at the problem:

The code is as follows:

$ F = 0.58;

Var_dump (intval ($ f * 100); // for output 57

I believe many of you have such questions.

For more information about the principles, see the "laruence" article, which provides a detailed explanation: answers to a frequently asked question about the PHP floating point number.

So how can we avoid this problem?

There are many methods. here are two examples:

1. sprintf

The code is as follows:

Substr (sprintf ("%. 10f", ($ a/$ B), 0,-7 );

2. round (note that it will be rounded off)

The code is as follows:

Round ($ a/$ B, 3 );

Or you can leave a message to tell me a better solution.

III. answers to a common PHP floating point number problem

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:

The code is as follows:

  

$ F = 0.58;

Var_dump (intval ($ f * 100); // for output 57

?>

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

The binary representation of 0.58 is basically (52 bits): 0010100011110101110000101000111101011100001010001111

The binary representation of 0.57 is basically (52 bits): 0010001111010111000010100011110101110000101000111101

The binary values of the two are:

The code is as follows:

0.58-> 0.57999999999999996

0.57-> 0.56999999999999995

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 .....

This article mainly introduces the summary of PHP floating point precision issues. This article focuses on the loss of PHP floating point precision issues and explains this in three different ways...

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.