Números de ponto flutuante (também conhecidos como "floats", "doubles" ou "números reais"), podem ser
especificados utilizando qualquer uma das seguintes sintaxes:
<?php $a = 1.234; $b = 1.2e3; $c = 7E-10; $d = 1_234.567; // a partir do PHP 7.4.0 ?>
Formalmente a partir do PHP 7.4.0 (anteriormente, sublinhados não eram permitidos):
O tamanho de um número de ponto flutuante depende da plataforma, sendo o máximo de ~1.8e308 com
precisão de 14 dígitos decimais um valor comum (número de 64 bits no formato
IEEE).
Aviso
Precisão de números de ponto flutuante
Números de ponto flutuante tem precisão limitada. Embora dependa do sistema,
o PHP geralmente utiliza o formato de precisão dupla do IEEE 754, que
trará uma precisão máxima devida a arredondamentos da ordem de 1.11e-16.
Operações matemáticas incomuns poderão ocasionar erros maiores, e, claro,
a propagação de erros deve ser considerada quando várias operações
forem realizadas.
Além disso, números racionais que têm representação exata em números
em base 10, como 0.1 ou
0.7, não possuem representação exata em ponto flutuante
na base 2, o formato utilizado internamente, não importando o tamanho
da mantissa. Portanto não existe conversão para o formato interno sem
uma pequena perda de precisão. Isso pode ocasionar resultados
confusos: por exemplo, floor((0.1+0.7)*10) normalmente
retornará 7, em vez do resultado esperado 8,
porque a representação interna final será algo como
7.9999999999999991118....
Então, nunca confie em resultados com números de ponto flutuante até a última casa, e
nunca compare números de ponto flutuante em igualdades. Se você realmente
precisar de alta precisão, você pode utilizar as funções matemáticas de precisão arbitrária
e as funções gmp estão disponíveis.
Para uma explicação "simples" dessa questão, veja o » guia sobre ponto flutuante,
que também tem o título alternativo de "Porque meus números não somam direito?".
Para valores de outros tipos, a conversão é realizada convertendo o
valor para int primeiro e em seguida para float. Veja
Convertendo para inteiro
para mais informações.
Nota:
Como certos tipos têm comportamento indefinido ao converter para
int, este também é o caso ao converter para
float.
Como notado acima, testar números de ponto flutuante com igualdade é
problemático, por causa da maneira como são representados internamente. Entretanto
existem maneiras de fazer comparações com números de ponto flutuante que
contornam essas limitações.
Para testar números de ponto flutuante, utilize um "valor de erro máximo"
na comparação utilizada. Esse valor é também chamado de epsilon,
ou unidade de erro, e deve ser a diferença mínima aceitável no resultado dos cálculos.
$a e $b serão consideradas iguais
até o 5º dígito de precisão.
Algumas operações numéricas podem resultar em valores representados pela constante
NAN. Esse resultado representa um valor desconhecido
ou não representável nos cálculos de ponto flutuante. Qualquer comparação frouxa
ou restrita deste valor com qualquer outro, inclusive ele mesmo, com exceção de true, terá
como resultado false.
Como o NAN representa um resultado irrepresentável,
NAN não deve ser comparado com outros valores, incluindo
ele mesmo, em vez disso, deve-se checá-lo utilizando a função is_nan().
General computing hint: If you're keeping track of money, do yourself and your users the favor of handling everything internally in cents and do as much math as you can in integers. Store values in cents if at all possible. Add and subtract in cents. At every operation that wii involve floats, ask yourself "what will happen in the real world if I get a fraction of a cent here" and if the answer is that this operation will generate a transaction in integer cents, do not try to carry fictional fractional accuracy that will only screw things up later.
just a comment on something the "Floating point precision" inset, which goes: "This is related to .... 0.3333333."
While the author probably knows what they are talking about, this loss of precision has nothing to do with decimal notation, it has to do with representation as a floating-point binary in a finite register, such as while 0.8 terminates in decimal, it is the repeating 0.110011001100... in binary, which is truncated. 0.1 and 0.7 are also non-terminating in binary, so they are also truncated, and the sum of these truncated numbers does not add up to the truncated binary representation of 0.8 (which is why (floor)(0.8*10) yields a different, more intuitive, result). However, since 2 is a factor of 10, any number that terminates in binary also terminates in decimal.
I'd like to point out a "feature" of PHP's floating point support that isn't made clear anywhere here, and was driving me insane.
This test (where var_dump says that $a=0.1 and $b=0.1)
if ($a>=$b) echo "blah!";
Will fail in some cases due to hidden precision (standard C problem, that PHP docs make no mention of, so I assumed they had gotten rid of it). I should point out that I originally thought this was an issue with the floats being stored as strings, so I forced them to be floats and they still didn't get evaluated properly (probably 2 different problems there).
To fix, I had to do this horrible kludge (the equivelant of anyway):
if (round($a,3)>=round($b,3)) echo "blah!";
THIS works. Obviously even though var_dump says the variables are identical, and they SHOULD BE identical (started at 0.01 and added 0.001 repeatedly), they're not. There's some hidden precision there that was making me tear my hair out. Perhaps this should be added to the documentation?
Be careful when using float values in strings that are used as code later, for example when generating JavaScript code or SQL statements. The float is actually formatted according to the browser's locale setting, which means that "0.23" will result in "0,23". Imagine something like this:
This would result in a different result for users with some locales. On most systems, this would print:
var foo = doBar(0.23);
but when for example a user from Germany arrives, it would be different:
var foo = doBar(0,23);
which is obviously a different call to the function. JavaScript won't state an error, additional arguments are discarded without notice, but the function doBar(a) would get 0 as parameter. Similar problems could arise anywhere else (SQL, any string used as code somewhere else). The problem persists, if you use the "." operator instead of evaluating the variable in the string.
So if you REALLY need to be sure to have the string correctly formatted, use number_format() to do it!
In some cases you may want to get the maximum value for a float without getting "INF".
var_dump(1.8e308); will usually show: float(INF)
I wrote a tiny function that will iterate in order to find the biggest non-infinite float value. It comes with a configurable multiplicator and affine values so you can share more CPU to get a more accurate estimate.
I haven't seen better values with more affine, but well, the possibility is here so if you really thing it's worth the cpu time, just try to affine more.
Best results seems to be with mul=2/affine=1. You can play with the values and see what you get. The good thing is this method will work on any system.
The 'floating point precision' box in practice means:
<? echo (69.1-floor(69.1)); ?> Think this'll return 0.1? It doesn't - it returns 0.099999999999994
<? echo round((69.1-floor(69.1))); ?> This returns 0.1 and is the workaround we use.
Note that <? echo (4.1-floor(4.1)); ?> *does* return 0.1 - so if you, like us, test this with low numbers, you won't, like us, understand why all of a sudden your script stops working, until you spend a lot of time, like us, debugging it.
But, please don't use your own "functions" to "convert" from float to binary and vice versa. Looping performance in PHP is horrible. Using pack/unpack you use processor's encoding, which is always correct. In C++ you can access the same 32/64 data as either float/double or 32/64 bit integer. No "conversions".
To get binary encoding: <?php $float32 = pack("f", 5300231); $binarydata32 =unpack('H*',$float32); //"0EC0A14A"
//Please consider the following code printf("%.53f\n",0.7+0.1); // 0.79999999999999993338661852249060757458209991455078125
var_dump(0.7+0.1); // float(0.8)
var_dump(0.799999999999999); //float(0.8)
var_dump(0.7999999); // float(0.7999999)
//Conclusion: PHP can support up to 53 decimal places, but in some output functions such as var_ Dump, when outputting decimals exceeding 14 places, will round off the 15th place, which causes significant misleading //experimental environment:linux x64,php7.2.x