97.3x4 != 389.2?

i have a multiplication of 2 numbers and an input box so that the user can write the answer;the code for the numbers is:
on (release) {
&nbsp &nbsp &nbsp &nbsp var c=random(1000)0.1 //first number: <1000 which i want with one decimal
&nbsp &nbsp &nbsp &nbsp var d=random(100) //second number which i want <100
&nbsp &nbsp &nbsp &nbsp var e = “”; // name of the input box where the user writes the answer
&nbsp &nbsp &nbsp &nbsp var res = c
d; //the result
&nbsp &nbsp &nbsp &nbsp trace(res);
}

Now i have a button that displays whether the answer is correct or not; the code is:
on (release) {
&nbsp &nbsp &nbsp &nbsp var certo; // dynamic text box variable which says whether the answer is correct or not
&nbsp &nbsp &nbsp &nbsp if (res != e) {
&nbsp &nbsp &nbsp &nbsp certo = “wrong…”;
&nbsp &nbsp &nbsp &nbsp }
&nbsp &nbsp &nbsp &nbsp else {certo = “right!”;
&nbsp &nbsp &nbsp &nbsp }
}

I trace the result so i can easily compare results; but for some strange reason, one time and another the answer says diferent things from the true result, or, on another words, says wrong when should say right, like:
97.3x4=389.2
says its a wrong answer.
Where is the mistake?
Can anyone help?

Wild guess in nature : maybe it’s a rounding trouble. You could try to go a decimal further. But maybe that’s not the problem at all…

pom 0]

flash has trouble with floating point numbers. if you doubt this, try this code on a movie clip:
onClipEvent(enterFrame){
trace(nNumber+=0.05);
}
as far as i can tell, this error shows up with any number smaller than 0.5!!
:slight_smile:
jeremy

I suppose you could multiply your float by ten, multiply, and then divide the result by ten. That could solve your problem.

pom 0]

someone reminded me that anything inside a textbox is a string; so, i should use parseFloat() when comparing results; it worked fine…so far

pom has the right idea. i had a project where i needed to increment by 0.25 and i kept having problems with it. what i ended up doing is incrementing by 1 and dividing the results by 4. it worked for that particular situation.
:slight_smile:
jeremy

as you can see in my original code i generated an integer and then divided it by 10, but the probs went on; this way, so far, i’ve no probs

Nice footer Sinfiniti! =)

Cheers!
-Niann

What if you generate your numbers, multiply them and THEN divide by ten ? Won’t it work better ??

pom 0]

thanks niann!
anyways, like i said before. if you use integers and then divide that error doesn’t show up. this error only seems to show up when incrementing!!
try this:
for(ii=97;ii<100;ii+=0.1){
trace(ii);
}
the error shows up!
but
trace(97.34);
will show 389.2
but, if you did this:
for(ii=95;ii<98;ii+=0.1){
trace(ii
4);
}
where it should say 389.2 it says 389.1999999999, not a correct answer.
bugs suck.
:slight_smile:
jeremy