PDA

View Full Version : 97.3x4 != 389.2 ???



rs2002
March 28th, 2002, 06:38 PM
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?

ilyaslamasse
March 28th, 2002, 07:46 PM
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]

sinfiniti
March 28th, 2002, 08:18 PM
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!!
:)
jeremy

ilyaslamasse
March 29th, 2002, 06:28 AM
I suppose you could multiply your float by ten, multiply, and then divide the result by ten. That could solve your problem.

pom 0]

rs2002
March 29th, 2002, 08:06 AM
someone reminded me that anything inside a textbox is a string; so, i should use parseFloat() when comparing results; it worked fine...so far

sinfiniti
March 29th, 2002, 10:04 AM
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.
:)
jeremy

rs2002
March 29th, 2002, 10:24 AM
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

Niann
March 29th, 2002, 12:17 PM
Nice footer Sinfiniti! =)

Cheers!
-Niann

ilyaslamasse
March 29th, 2002, 08:29 PM
What if you generate your numbers, multiply them and THEN divide by ten ? Won't it work better ??

pom 0]

sinfiniti
March 29th, 2002, 11:08 PM
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.3*4);
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.
:)
jeremy