Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
How do I get out of the "While" Method?
Old
Hoshi_Kawa
Guest
 
Status:
Posts: n/a
Default How do I get out of the "While" Method? - 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?
Attached Files
File Type: a2w random number.a2w (1.59 MB, 436 views)
File Type: a2w GuessNumber.a2w (390.5 KB, 437 views)
   
Reply With Quote
Old
DrJim
Guest
 
Status:
Posts: n/a
Default 10-16-2006, 07:15 PM

This looks like another case (see http://www.alice.org/community/showt...=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?
   
Reply With Quote
I believe the cause is rooted in the way alice compares numbers
Old
newdok
Guest
 
Status:
Posts: n/a
Default I believe the cause is rooted in the way alice compares numbers - 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);
}
}
   
Reply With Quote
Old
Shadow Sovereign
Guest
 
Status:
Posts: n/a
Question 12-01-2006, 09:24 PM

Newdok, your avatar has some code that looks somewhat familiar to me... where have I seen that before?

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?( ) Not sure if I'm being much help...
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2017, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.