PHP floating-point Precision Value instance program detailed

Source: Internet
Author: User
Tags arithmetic

The precision value of a floating-point number in PHP is used to control the output of the floating-point number. Can be understood as the control of the output of the number of digits, the difference in precision, see the output may also be different, note: its internal or in accordance with the actual value of storage, when two floating-point number for arithmetic, with its original value.

PHP's configuration file uses precision to set the precision value of the global specified floating-point number, it seems that each distribution, its default settings are not the same, I saw in the window is 12, under Linux see this value is 14, of course, you can use the program Ini_ Set to change global settings, such as:

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

I have always understood the accuracy of the decimal point after the retention of how much, then in the PHP floating-point number is this? The answer is in the negative.

Floating point numbers are actually part of an integer and a decimal part, where the precision is that the integer part of the number of digits plus the decimal part of the number of digits can not exceed the maximum precision, if more than the rounded method to truncate to the maximum precision value. If the integer part is 0, the number of digits is not counted, and the end 0 of the fractional part does not count in digits. In addition, for the same number, the difference between precision, may show the form is not the same. Here's how to illustrate this by example.

The whole number of parts is divided into 0 cases

The code is as follows Copy Code

$num = 0.12345600000000000;
The whole number is divided into 0, the number of digits is 0, and the 0 at the end of the fractional part does not count in digits, so the total digits are 6.

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

Ini_set ("Precision", "3");
Echo $num; 0.123
exceeds the precision value and retains 3 bits

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

exceeds the precision value and retains 5-bit. In this case, the precision value is equivalent to keeping several digits after the decimal point.

The whole number of parts is greater than 0 condition

The code is as follows Copy Code

$num = 12.12345600000000000;
The whole number of parts is divided into 12, the number of digits is 2, the 0 of the end of the fractional part does not count in digits and the digits are 6, so the total digits are 2 + 6.

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

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

Ini_set ("Precision", "5");
Echo $num; 12.123
exceeds the precision value, the integer part bit is 2, so only 3 digits are retained to see the number of digits reserved after the decimal point and the number of digits in the precision already integral part

The whole number of parts is greater than 0 of the case II

  code is as follows copy code

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

 ini_set ("precision", "12");
 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 the precision value, and the integer part bit exceeds the precision, the fractional part is discarded, and the integer part takes only 3 bits

 ini_set ("Precision", "5");
 echo $ Num 12346000

exceeds the precision value, and the integer partial digits exceed the precision, the fractional part discards, and the integer part takes only 5 digits to see in the above example, the precision value also relates to the intercept of the integer part. Note that the last two examples show different ways, one is to use scientific notation, one is back with 0 complement. The conclusion is that when the integer part of the number of digits minus the precision value is greater than 4, the use of scientific notation, or the following 0 complement, in other words, if the integer part of the number of digits exceeds the precision, after truncation, the number of the complement 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 results of the output are:
/*
1331625700
1331625700
-1.247
*/

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

PHP built-in Echo, Var_dump, Json_encode, string concatenation functions (instructions) in the display floating-point numbers are problematic, resulting in loss of precision.

The code is as follows Copy Code

<?php

$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, the most convenient way to use PHP to convert floating-point numbers to strings or display is not possible, you must use printf/sprintf to convert floating-point numbers to strings.

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.