Re: [PHP-ES] Problema con una simple suma ...
[email protected] ("hackob")
| Newsgroups | php.general.es |
|---|---|
| Message-ID | <1626609112-1270773850-cardhu_decombobulator_blackberry.rim.net-1129068147-@bda002.bisx.prod.on.blackberry> |
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