Lecture
Double - a real number of fairly high accuracy (it should be enough for the vast majority of mathematical calculations).
Floating-point numbers (double-precision numbers or real numbers) can be defined using any of the following syntaxes:
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
The formal structure of scalar type float is:
LNUM [0-9]+
DNUM ([0-9]*[.]{LNUM}) | ({LNUM}[.][0-9]*)
EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})
The size of the whole depends on the platform, although the maximum is usually ~ 1.8e308 with an accuracy of about 14 decimal digits (this is a 64-bit IEEE format).
Quite often, simple decimal fractions like 0.1 or 0.7 cannot be converted to their internal binary counterparts without a small loss of accuracy. This can lead to unexpected results: for example, floor ((0.1 + 0.7) * 10) will most likely return 7 instead of the expected 8 as the result of the internal representation of a number that is in fact something like 7.9999999999 ....
This is due to the inability to accurately express some fractions in decimal notation by a finite number of digits. For example, 1/3 in decimal form takes the form 0.3333333. . . .
So never trust the accuracy of the last digits in the results with floating point numbers and never check them for equality. If you really need high precision, you should use the mathematical functions of arbitrary precision or gmp-functions.
For information on when and how strings are converted to floating-point numbers, see Converting Strings to Numbers. For values of other types, the conversion will be the same as if the value was first converted to an integer, and then to a floating point number. For more information, see Conversion to Integer.
Comments
To leave a comment
Running server side scripts using PHP as an example (LAMP)
Terms: Running server side scripts using PHP as an example (LAMP)