Re: [PHP-ES] Problema con una simple suma ...

[email protected] ("J. Mauro Daino")
Newsgroups php.general.es
Message-ID <[email protected]>
Hola hackob:

2010/4/8 hackob <[email protected]>

> El problema se solucionó cuando modifiqué el parámetro precision en el
> php.ini de 12 a 15, lo curioso es que si pongo 16 el resultado es
> 0.099999999999999998.
>
> Me gustaría saber el por qué ...
>

Copio y pego desde http://php.net/manual/en/language.types.float.php:

The size of a float is platform-dependent, although a maximum of ~1.8e308
with a precision of roughly 14 decimal digits is a common value (the 64 bit
IEEE format).
*Warning*Floating point precision

It is typical that simple decimal fractions like *0.1* or *0.7* cannot be
converted into their internal binary counterparts without a small loss of
precision. This can lead to confusing results: for example, *
floor((0.1+0.7)*10)* will usually return *7*instead of the expected *8*,
since the internal representation will be something like *7.9*.

This is due to the fact that it is impossible to express some fractions in
decimal notation with a finite number of digits. For instance, *1/3* in
decimal form becomes *0.3*.

So never trust floating number results to the last digit, and never compare
floating point numbers for equality. If higher precision is necessary,
the arbitrary
precision math functions <http://php.net/manual/en/ref.bc.php> and
gmp<http://php.net/manual/en/ref.gmp.php> functions
are available.
O sea, si queres una suma de precisión no uses el operador "+" sino la
función "bcadd()" o "gmp_add()". Suerte.

-- 
J. Mauro Daino (tcc: jMd)
* profile: google.com/profiles/maurodaino
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.