Hola.

On 16/07/2007, at 15:00, Juan José Montes de Oca Arbós wrote:

> Buenas a todos... a ver si alguno conoce una solución para el  
> siguiente
> problema.
>
> Tanto en IE 6 como en FF2, al multiplicar 0.1 * 3 en lugar de
> devolverme 0.3devuelve
> 0.30000000000000004.
>
> Estuve leyendo varios articulos de internet, y encontré que es una  
> problema
> en la forma de representar los números, y hay ejemplos que utilizando
> algunos tipos de datos en ciertos lenguajes se soluciona, pero en  
> JS no
> encontré forma de solucionarlo... ¿alguien sabe como solucionarlo?


Lamentablemente no hay solución. Snif.

Y efectivamente, el problema radica en la forma que tiene ECMAScript  
de trabajar con números. Cito <http://www.thescripts.com/forum/ 
post2022038-5.html>

 >>>
The problem is that Javascript is using 32 bits of data which have  
4294967296 different combinations to hold any value in the range

1.7976931348623158e+308 to 2.2250738585072014e–308

It does this by using a smaller presision value and using some of the  
bits as a exponent (this can also be done in 16 bits with a smaller  
range and less presision) which results in it being able to  
approximate any value in the range but not exactly represent them all  
(because in real number terms the are an infinaite number of values  
between any 2 given values).

As part of your calculcation clearly the internal representation of  
the value is going outside the available presision (15 digits for a  
32 bit number but only 6 digits for a 16 digit number so perhaps  
Javascript uses 16 bit floating point values) and you are ending up  
with an approximation to the value instead of an exact value.
<<<

Si conoces la precisión final que necesitas, puedes apañarte usando  
Number.toFixed(n), usia

        (0.1 * 3).toFixed(1);

Si no la conoces... mejor será que uses un lenguaje preciso si de  
hacer cuentas precisas se trata.

Salud.
-- 
Choan Gálvez
<[EMAIL PROTECTED]>
<http://choangalvez.nom.es/>



_______________________________________________
javaEScript mailing list
javaEScript@scriptia.net
http://lists.scriptia.net/listinfo.cgi/javaescript-scriptia.net

Responder a