View Full Version : How do I get out of the "While" Method?
Hoshi_Kawa
10-16-2006, 08:36 AM
I'm currently in class, and were trying do question #10 for Chapter 5, where we have to create a Alice Guessing game.
Apparently while trying to use the "While" method we cannot come out of it. My teacher was able to get it to work on her computer, but she's showing us how she did it, but it's still not comming out of the "While" Method for us.
Our teachers program is called random number.a2w
mine is GuessNumber.a2w
can anyone help us out?
DrJim
10-16-2006, 07:15 PM
This looks like another case (see http://www.alice.org/community/showthread.php?p=1306#post1306 ) of Alice using integers but not allowing them as a data type (see bug list on the main Alice page). You can solve your problem by multiplying the random number by 1.0 (not 1) which will convert it to double precision (or floating point) and your program should work fine - at least it did for me. I can't see any reason your teacher's program doesn't have the same problem, but it also worked for me without any changes - anyone else see the difference?
:confused:
newdok
12-01-2006, 08:22 PM
a bit of background is nessesary. Alice stores the Number data type as a "double precision floating point number" (aka a double), and the troublesome thing about floating point comparisons is that due to the underlying implementation of floating point numbers, numbers that are extremely close might not actually be equal. anyways when i learned C a few years back, we had to deal with similar issues. What they taught us was to never compare floating point numbers for equality directly. Instead, subtract the smaller from the larger, and see if that value is less than some known quantity. Note that you have to get the sign right or else you'll end up with it being true if the second number is greater than the first number by not less than epsilon.
//example:
/**
*@author John O'Meara
* example of a proper equality comparison for floating point numbers...
*/
public Boolean areNumbersEqual(Number a, Number b)
{
Number ZERO_TOLERANCE = 0.01;
// ^note: set this to a sufficently low value
//for whatever your purposes are( - in otherwords, this should be larger
//than any difference values that you care to differentation between.
if (a > b)
{
return ((a-b)<ZERO_TOLERANCE);
}
else
{
return ((b-a)<ZERO_TOLERANCE);
}
}
Shadow Sovereign
12-01-2006, 09:24 PM
Newdok, your avatar has some code that looks somewhat familiar to me... where have I seen that before? :confused:
I'm not sure what you're asking here, Hoshi-Kawa... are you trying to use a random number to find a certain integer within a limited scale, or just a wildly random number, or something?(:confused: ) Not sure if I'm being much help... :(