Home >Backend Development >PHP Tutorial >PHP learning record (floating point type)_PHP tutorial
$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.