Click to See Complete Forum and Search --> : Need workaround for JavaScript bug I found


Gambit
05-12-2003, 10:12 AM
I tested this code in Internet Explorer 6, and Netscape 4.8, and both display the results incorrectly.

Notice I have it make 3 calculations, the first and last are incorrect, while the second is correct. It really confuses me as to why it is displaying incorrect result sometimes, but not others.

Could you guys (and gals) take a look and see if you can tell me what is going wrong?


<HTML>
<HEAD>
<TITLE>JavaScript Test</TITLE>
</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript">
<!--

var kilogram = 1;
var microgram = 1.0e-9;
var nanogram = 1.0e-12;
var picogram = 1.0e-15;

function convert(FromValue, ToValue, Qty)
{
var ConversionFactor = eval(FromValue + "/" + ToValue);
//var ConversionFactor = FromValue / ToValue;
return "" + (Qty * ConversionFactor);
}

document.write("<p>Convert 1 Kilograms to Micrograms:<br>" +
"Correct Answer:<br>1000000000<br>Function returned:<br>" +
convert(kilogram, microgram, 1) + "</p>");
document.write("<p>Convert 1 Kilograms to Nanograms:<br>" +
"Correct Answer:<br>1000000000000<br>Function returned:<br>" +
convert(kilogram, nanogram, 1) + "</p>");
document.write("<p>Convert 1 Kilograms to Picograms:<br>" +
"Correct Answer:<br>1000000000000000<br>Function returned:<br>" +
convert(kilogram, picogram, 1) + "</p>");

//-->
</SCRIPT>

</BODY>
</HTML>

AdamGundry
05-12-2003, 11:43 AM
Javascript can only handle numbers between 5e-324 and about 1.798e+308. I'm guessing your function tries to use numbers outside that range, which may be the cause of the problem.

Could you reduce the values of the numbers to keep them within the required range?

Adam

khalidali63
05-12-2003, 12:13 PM
You may have to make changes to this line

var ConversionFactor = Math.ceil(eval(FromValue + "/" + ToValue);

to this

var ConversionFactor = Math.ceil(eval(FromValue + "/" + ToValue));

It seems like when performing division its return a float point value in some cases,if it will return a float point value your results will include a decimal point.to control that you may want to use Math.ceil() function..as shown above...

Gambit
05-13-2003, 03:08 AM
Thanks for trying, but nothing to help yet.

The numbers I am using are not outside the extreme. 1.0e-9 fails, but 1.0e-12 works. So it is not a problem of them being too small or too big.

I can't use the Math.ceil function, since some calculations must return decimal, for example, pounds to kilograms would have to return a float.

I managed to reproduce the bug with just one line of code.

document.write(eval("1/1.0e-9"));

I also tried

document.write(eval("1/0.000000001"));

I also tried without the eval

document.write(1/0.000000001);

All of the return incorrect results. It returns 9999999.9999999 when if you use your calculator you see it should return 1000000000

Any ideas????

Fang
05-13-2003, 06:14 AM
This is not strictly a bug, but a limitation of floating point numbers in general. See http://www.merlyn.demon.co.uk/js-round.htm
You could try (kilogram/picogram).toFixed(0) which would give the correct answers in your example, but if you want to do more complicated conversions (ie. fractions) then the number of decimal places will be required.

khalidali63
05-13-2003, 09:19 AM
looks like (1/0.000000001).toFixed(2) will be the closes you could get..