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

[email protected] (Jacobo Pedrosa)
Newsgroups php.general.es
Message-ID <[email protected]>
Se me ocurre que puede ser cosa de simple redondeo. Es decir, reduzcamos
decimales.

Si coges el valor 0,09 y pones solo 1 decimal, normalmente te devuelve 0,1.
Si lo haces con 0,099 y 2 decimales, te seguirá diciendo 0,1, con 3 0,99
...
creo que es lo mismo que te pasa a ti solo que con 14 decimales.




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

> Curiosamente con un valor de 14 sigo con el problema, sin embargo si subo a
> 15 da el resultado esperado.
>
> Gracias
> @hackob
> hackob [ [email protected] ]
>
> -----Original Message-----
> From: "J. Mauro Daino" <[email protected]>
> Date: Thu, 8 Apr 2010 21:18:25
> To: php-list<[email protected]>
> Subject: Re: [PHP-ES] Problema con una simple suma ...
> 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.