Home >Backend Development >PHP Tutorial >Summary of PHP floating point number usage and problems_PHP tutorial

Summary of PHP floating point number usage and problems_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:49:30819browse

There are many floating-point number processing functions in PHP, including ceil, floor, round, and intval. After processing by them, we can return the decimal places we want for the floating-point number. Let me introduce some usage of floating-point numbers in PHP. and problem analysis.

1. How to use PHP floating point numbers

PHP floating point type rounding ceil — further rounding
Description
float ceil (float value)
Returns the next integer that is not less than value. If value has a decimal part, it is rounded up. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer.

Example 1. ceil() example

The code is as follows Copy code
 代码如下 复制代码

< ?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
?>

< ?php

echo ceil(4.3); // 5

echo ceil(9.999); // 10

?>


PHP floating point type rounding floor - rounding by rounding method

Description

float floor (float value)
 代码如下 复制代码

< ?php
echo floor(4.3); // 4
echo floor(9.999); // 9
?>

Returns the next integer not greater than value, with the decimal part of value rounded off. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.


Example 1. floor() example

The code is as follows Copy code

< ?php

echo floor(4.3); // 4

echo floor(9.999); // 9

?>
 代码如下 复制代码


< ?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 rounding — rounding floating point numbers
Description

float round ( float val [, int precision] )
 代码如下 复制代码
< ?php
echo intval(4.3); //4
echo intval(4.6); // 4
?>
Returns val rounded to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).

Example 1. round() example

The code is as follows Copy code

< ?php

echo round(3.4); // 3
代码如下 复制代码

  

   $a = 1315537636.338467;

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

   echo $a . "n";

   echo $a; echo "n";

  ?>

结果

  1315537636.338467

  1315537636.3385

  1315537636.3385

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 rounding intval—convert variables into integer types Example intval()
The code is as follows Copy code
< ?php<🎜> echo intval(4.3); //4<🎜> echo intval(4.6); // 4<🎜> ?>
2. Convert floating point number to string PHP’s built-in echo, var_dump, json_encode, string concatenation and other functions (instructions) have problems when displaying floating point numbers, resulting in loss of precision.
The code is as follows Copy code
  <🎜>  $a = 1315537636.338467;<🎜> <🎜> printf("%f", $a); echo "n";<🎜> <🎜> echo $a . "n";<🎜> <🎜> echo $a; echo "n";<🎜> <🎜> ?> Results  1315537636.338467  1315537636.3385  1315537636.3385

In other words, it is not possible to use the most convenient method of PHP to convert floating point numbers into strings or display them. You must use printf/sprintf to convert floating point numbers into strings.


Three, the answer to a common question

However, I missed one thing, which is the answer to this common question:

The code is as follows Copy code
 代码如下 复制代码

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

$f = 0.58;

var_dump(intval($f * 100));

?>

Why is the output 57

Why is the output 57? Is it a PHP bug?

I believe that many students have had such questions, because there are many people asking me similar questions, not to mention that people often ask 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 64-bit length (double precision) as an example, will be represented by 1 sign bit (E), 11 exponent bits (Q), and 52-bit mantissa (M) (a total of 64 bits).

Sign bit: The highest bit represents the sign of the data, 0 represents a positive number, and 1 represents a negative number.

Exponent bit: indicates the data raised to the power of base 2, and the exponent is represented by an offset code

Mantissa: Indicates the significant digits after the decimal point of the data.
The key point here is the representation of decimals in binary. As for how decimals are represented in binary, you can search on Baidu. I won’t go into details here. The key thing we need to understand is that for binary representation, 0.58 is infinite. Long values ​​(numbers below omit 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 numbers of the two, if calculated only through these 52 bits, are:

0.58 -> 0.57999999999999996
0.57 -> 0.56999999999999995

As for the specific floating point multiplication of 0.58 * 100, we will not consider it in detail. Those who are interested can look at the floating point. We will look at it vaguely through mental arithmetic... 0.58 * 100 = 57.999999999

Then if you intval it, it will naturally be 57… It can be seen that the key point of this problem is: "Your seemingly finite decimal is actually infinite in the binary representation of the computer"


4. The problem of float (floating point number) comparison

 代码如下 复制代码

$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);

When I was developing a contract management system recently, it involved comparing two floating point numbers, which made me very depressed.

A long time ago, I didn’t know where I heard the “truth” of “Don’t use the equal sign to compare floating point numbers”. I used it regularly, and it seemed that there was no problem. But this time the problem finally came.
 代码如下 复制代码

float(12300)
float(12300)
bool(false)

The code is as follows Copy code
The result is:
The code is as follows Copy code
float(12300) float(12300) bool(false)

I later learned that in PHP, to compare the sizes of two floating point numbers, you can use bccomp (parameter 1, parameter 2, decimal places) to compare.

$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));
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));

结果:

float(12300)
float(12300)
int(0) // 0表示两个浮点数值相等

Copy code

Result:

float(12300)
float(12300)
int(0) // 0 means two floating point values ​​are equal

For specific usage of bccomp function, please refer to the PHP manual http://www.bkjia.com/PHPjc/632704.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/632704.htmlTechArticleThere are many floating point number processing functions in php, including ceil, floor, round, intval, which are processed by them After that, we can return the decimal places we want for the floating point number. Let me introduce one...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn