Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of the accuracy of PHP floating-point numbers

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "summary of PHP floating-point accuracy problems". In daily operation, I believe many people have doubts about the summary of PHP floating-point accuracy problems. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "summary of PHP floating-point accuracy problems". Next, please follow the editor to study!

1. The loss of accuracy of PHP floating-point numbers

Take a look at the following code first:

$f = 0.57

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

The result may come as a surprise to you. PHP follows the IEEE 754 double precision:

Floating-point numbers, with 64-bit double precision, are represented by 1-bit symbolic bits (E), 11 exponential bits (Q), and 52-bit Mantissa (M) (a total of 64 bits).

Symbol bit: the highest bit represents the positive or negative of the data, 0 represents a positive number, and 1 represents a negative number.

Exponential bit: represents the power of the data with 2 as the base, and the exponent is expressed by offset code

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

Let's take a look at how decimals are expressed in binary:

Multiply by 2, arrange in order, that is, the decimal part is multiplied by 2, then the integer part is multiplied by 2, then the integer part is multiplied by 2, and the remaining decimal part is multiplied by 2, all the way to the decimal part, but if the decimal part like 0.57 goes on like this, the decimal part cannot be 0. The decimal of significant places is expressed in binary, but it is infinite.

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

If there are only 52 digits, 0.57 = "0.56999999999999995"

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

2. The accuracy of PHP floating-point numbers

Let's look at the question first:

The copy code is as follows:

$f = 0.58

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

I believe there are many students who have such questions.

The specific principles can be read in an article in Brother Bird, where there is a detailed explanation: the answer to a common question about PHP floating-point numbers.

So how to avoid this problem?

There are many ways to do this. Here are two:

1. Sprintf

The copy code is as follows:

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

2. Round (note that it is rounded)

The copy code is as follows:

Round ($a _ Universe _ blank, 3)

Or if you have a better idea, leave a message and let me know.

III. The answer to a common question about PHP floating point numbers

About PHP floating point numbers, I wrote an article before: about PHP floating point numbers you should know (All 'bogus' about the float in PHP)

However, I missed one point at that time, that is, the answer to the following frequently asked question:

The copy code is as follows:

Why is the output 57? PHP's bug?

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

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

Floating-point numbers, taking the length of 64 bits (double precision) as an example, will be represented by 1-bit symbol bit (E), 11 exponential bit (Q), 52-bit Mantissa (M) (a total of 64 bits).

Symbol bit: the highest bit represents the positive or negative of the data, 0 represents a positive number, and 1 represents a negative number.

Exponential bit: represents the power of the data with 2 as the base, and the exponent is expressed by offset code

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

The key point here is that decimals are represented in binary. About how decimals are expressed in binary, you can do it in Baidu. I'm not going to repeat them here. The key thing to understand is that 0.58 is an infinitely long value for binary representation (the following number omits the implied 1).

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

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

And the binary of the two, if only calculated through these 52 bits, are:

The copy code is as follows:

0.58-> 0.57999999999999996

0.57-> 0.56999999999999995

As for the specific floating-point multiplication of 0.58 * 100, we do not consider it in such detail. If you are interested, you can look at it (Floating point). We will vaguely look at it by mental calculation. 0.58 * 100 = 57.999999999

Then if you intval it, it will be 57. .

At this point, the study on the "summary of the accuracy of PHP floating-point numbers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report