PHP learning record (floating point type)_PHP tutorial

WBOY
Release: 2016-07-21 14:52:23
Original
967 people have browsed it

$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

The regular expression is as follows:

Floating point number: [0-9]+
Double type: ([0-9][.]{LNUM}) | ({LNUM}[.][0-9]*)
Exponential expression: [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})

==========================
The maximum floating point number php code is as follows:
function float_max($mul = 2, $affine = 1) {
$max = 1; $omax = 0;
While((string)$max != 'INF') { $omax = $max; $max *= $mul; }

for($i = 0; $i < $affine; $i++) {
$pmax = 1; $max = $omax;
While((string)$max != 'INF') {
         $omax = $max;
         $max += $pmax;
          $pmax *= $mul;
}
}
Return $omax;
}
echo "Maximum floating point number:" ;var_dump(float_max());
?>
=========================
The results are as follows (depending on the platform):
Maximum floating point number: float(1.79769313486E+308)

Notes on floating point precision:

Simple decimal fractions like 0.1 or 0.7 lose precision when converted to the internal binary format:
For example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8 because the internal representation of the result is actually 7.9.
It is simply impossible to express certain decimal fractions accurately with a finite number of digits.
For example, 1/3 in decimal becomes 0.3.
So
1. Never trust that floating point results are accurate to the last digit,
2. Also never compare two floating point numbers for equality.
3. If you really need higher precision, you should use arbitrary precision math functions or the gmp function.

As of PHP 5, if you try to convert an object to a float, an E_NOTICE error is issued.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371639.htmlTechArticle?php $a = 1.234; $b = 1.2e3; $c = 7E-10; ? Regular expression expression As follows: Floating point number: [0-9]+ Double precision type: ([0-9][.]{LNUM}) | ({LNUM}[.][0-9]*) Exponential expression: [+- ]?(({LNUM} | {DNU...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!