PHP floating point Precision Value Instance program detailed _php tutorial

Source: Internet
Author: User
This article to give you a brief introduction of the PHP floating point precision value of the instance program, I hope there is a need to understand the accuracy of floating point number of friends can enter the reference.

The precision value of floating-point numbers in PHP is used to control the output of the floating-point number, can be understood as the control of the output of bits, the accuracy of different values, see the output may not be the same, note: its internal or in accordance with the actual value of the storage, when two floating-point numbers are arithmetic, with its original value.

PHP configuration file using precision to set the global specified floating point precision value, it seems that each distribution, its default settings are not the same, I see under Window is 12, under Linux see this value is 14, of course, can also be used in the program Ini_ Set to change the global settings, for example:

The code is as follows Copy Code
Ini_set ("precision", "15");

For the accuracy I have always understood the number of reserved after the decimal point, then in the PHP floating point is this? The answer is in the negative.

Floating-point numbers are actually integers and fractional parts, where the precision is the number of digits in the integer part plus the fractional digits of the decimal part cannot exceed its maximum precision, and if it is exceeded, it is truncated to the maximum precision value by rounding the method. If the integer part is 0, the number is not counted, and the fractional end 0 is not counted as the number of digits. In addition, for the same number, precision different, may show the form of expression is not the same. Here is an example of how to illustrate this.

The whole number of parts is divided into 0 cases

The code is as follows Copy Code

$num = 0.12345600000000000;
The integer part is 0, the number of digits is 0, the end of the fractional part does not count toward the number of digits, so the total number of digits is 6

Ini_set ("precision", "12");
Echo $num; 0.123456
The precision value is not exceeded, showing the result is 0.123456

Ini_set ("Precision", "3");
Echo $num; 0.123
Over precision value, reserved 3 bits

Ini_set ("Precision", "5");
Echo $num; 0.12346

Exceeding the precision value, retaining 5 bits in this case, the precision value is equivalent to the number of decimal points retained.

Integer part greater than 0 case

The code is as follows Copy Code

$num = 12.12345600000000000;
The integer part is 12, the number of digits is 2, the end of the fractional part does not count toward the number of digits, the number of digits is 6, so the total number of digits is 2 + 6

Ini_set ("precision", "12");
Echo $num; 12.123456
The precision value is not exceeded, showing the result is 12.123456

Ini_set ("Precision", "3");
Echo $num; 12.1
exceeds the precision value, the integer part number is 2, so only one decimal place is reserved

Ini_set ("Precision", "5");
Echo $num; 12.123
Exceeding the precision value, the integer part number is 2, so only 3 decimal places can be seen the number of digits retained after the decimal point is related to the number of digits in the integer portion of the precision

The integer part is greater than 0 of the two

copy code

$num = 12345678.12345600000000000;
// The whole number is divided into 12345678, the number of digits is 8, the fractional part at the end of 0 does not count into the number of digits, the number of digits is 6, so the total number of digits is 8 + 6

Ini_set ("precision", "three");
echo $num;//12345678.1235
//exceeds the precision value and displays the result as 12345678.1235

Ini_set ("Precision", "3");
Echo $num;//1.23E+7
//exceeds precision value, and integer partial digits exceed precision, fractional part is discarded, and integer part only takes 3 bits

Ini_set ("Precision", "5");
echo $num;//1 2346000

Exceeding the precision value, and the integer partial digits exceed the precision, the fractional part is discarded, and the integer part takes only 5 bits the above example can be seen that the precision value is also related to the interception of the integer part. Note that the last two examples show different ways, one is to use the scientific notation, and the other is to use the 0 supplement later. The conclusion of the experiment is that when the number of digits in the integer part minus the precision value is greater than 4, the method of scientific counting is used, otherwise the 0 complement is used, in other words, the number of integers is more than the precision value, after truncation, 0 will not exceed 4.

Floating-point arithmetic

The code is as follows Copy Code

$num 1 = 1331625729.687;
$num 2 = 1331625730.934;
Ini_set ("precision", "8");

echo $num 1. '
';
Echo $num 2. '
';

$sub = $num 1-$num 2;

Echo $sub. '
';
The result of the output is:
/*
1331625700
1331625700
-1.247
*/

The above example shows that the precision value only controls the display result, the internal storage or the original value, so the value of the $sub is 1331625729.687 minus 1331625730.934.

PHP's built-in Echo, Var_dump, Json_encode, string concatenation and other functions (directives) are problematic 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

That is, using PHP's most comfortable way to convert a floating point number to a string or display is not possible, you must use printf/sprintf to convert the floating-point number to a string.

http://www.bkjia.com/PHPjc/632694.html www.bkjia.com true http://www.bkjia.com/PHPjc/632694.html techarticle This article to give you a brief introduction of the PHP floating point precision value of the instance program, I hope there is a need to understand the accuracy of floating point number of friends can enter the reference. The precision value of floating-point numbers in PHP is ...

  • Related Article

    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.