You get a bonus - 1 coin for daily activity. Now you have 1 coin

Float Numbers in Float

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).

Floating point precision

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.

Conversion to float

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.

created: 2016-01-25
updated: 2021-03-13
168



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)