PHP floating point usage and problem summary _ PHP Tutorial

Source: Internet
Author: User
PHP floating point usage and problem summary. There are many floating point number processing functions in php, including ceil, floor, round, and intval. after they are processed, they can return the expected floating point decimal places, next I will introduce a lot of php floating point processing functions, including ceil, floor, round, and intval. after they are processed, they can return the expected floating point decimal places, next I will introduce some usage and problem analysis of php floating point numbers.

I. php floating point usage

PHP floating point type integer ceil-one method integer
Description
Float ceil (float value)
Returns the next integer of no less than value. if the value has a decimal part, it is entered in one place. The type returned by ceil () is still float, because the float value range is usually larger than integer.

Example 1. ceil () example

The code is as follows:

<? Php
Echo ceil (4.3); // 5
Echo ceil (9.999); // 10
?>

Floor-rounding method for integer of PHP floating point type

Description
Float floor (float value)
Returns the next integer not greater than value and rounds the decimal part of value. The return type of floor () is still float, because the float value range is usually larger than that of integer.

Example 1. floor () example

The code is as follows:

<? Php
Echo floor (4.3); // 4
Echo floor (9.999); // 9
?>

Round of PHP floating point type-round of floating point number
Description
Float round (float val [, int precision])
Returns the result of rounding val by the specified precision (number of digits after decimal point. Precision can also be negative or zero (default ).

Example 1. round () example

The code is as follows:


<? Php
Echo round (3.4); // 3
Echo round (3.5); // 4
Echo round (3.6); // 4
Echo round (3.6, 0); // 4
Echo round (1.95583, 2); // 1.96
Echo round (1241757,-3); // 1242000
Echo round (5.045, 2); // 5.05
Echo round (5.055, 2); // 5.06
?>

PHP floating point type integer intval-convert variable to integer type
Example intval ()

The code is as follows:
<? Php
Echo intval (4.3); // 4
Echo intval (4.6); // 4
?>

2. convert a floating point to a string

PHP built-in echo, var_dump, json_encode, string concatenation and other functions (commands) have problems when displaying floating point numbers, resulting in loss of precision.

The code is as follows:

  

$ A = 1315537636.338467;

Printf ("% f", $ a); echo "n ";

Echo $ a. "n ";

Echo $ a; echo "n ";

?>

Result

1315537636.338467

1315537636.3385

1315537636.3385

That is to say, it is impossible to convert a floating point number to a string or display it in PHP's best way. you must use printf/sprintf to convert a floating point number to a string.


3. answers to a common question

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

The code is as follows:

$ F = 0.58;
Var_dump (intval ($ f* 100 ));
?>

Why 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:
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"

4. float (floating point number) comparison

Recently, when I was developing a contract management system, two floating point numbers were involved, which made me miserable.
A long time ago, N did not know where to hear a "truth" that "do not use equal signs to compare floating-point numbers". he was using it at ordinary times. It seemed that there was no problem, but this problem finally came.

The code is as follows:

$ Sum = "12300.00 ";
$ A = "10000.30 ";
$ B = "2000.30 ";
$ C = "299.40 ";

$ Sum = (float) $ sum;
$ S = (float) ($ a + $ B + $ c );
Var_dump ($ sum, $ s );
Var_dump ($ sum = $ s );

The result is:

The code is as follows:

Float (12300)
Float (12300)
Bool (false)

Later I learned that in PHP, to compare the two floating point numbers, we can use bccomp (parameter 1, parameter 2, decimal place) to compare them.

The code is as follows:

$ Sum = "12300.00 ";
$ A = "10000.30 ";
$ B = "2000.30 ";
$ C = "299.40 ";

$ Sum = (float) $ sum;
$ S = (float) ($ a + $ B + $ c );
Var_dump ($ sum, $ s );
Var_dump (bccomp ($ sum, $ s, 2 ));

Result:

Float (12300)
Float (12300)
Int (0) // 0 indicates that two floating point values are equal

Use of bccomp functions in PHP Manual

After processing the secondary node, they can return the expected floating point decimal point. I will introduce the following...

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.