FUNCTION:
IsASquare(). This function takes an integer as
its argument. If its argument is a perfect square (that is, its
square root has no decimal part), it returns TRUE (1), otherwise it
returns FALSE (0). The integer can be any of the usual IBasic types, as shown below: 
Type

Exponent
of 2 
Value

int  2^{30}  1073741824 
uint  2^{31}  2147483648 
int64  2^{61}  2305843009213693952 
uint64  2^{62}  4611686018427387904 
Declaring the function must be done like
this: declare IsASquare(r: int) (or whatever type of integer you choose). Here's the code: sub IsASquare(s) ' if arugument s is a perfect square ' returns TRUE (1), otherwise ' returns FALSE (0) def root: float root = sqrt(s) if root = int(root) return 1 else return 0 end 
Variable root is created to hold the square
root of the function's argument, s.
Now, s is actually an
integer, so taking its square root in the usual way would result in
another integer. For instance, if the code says something like "s = sqrt(s)" for a value of, say, 50, s would end up as 7, instead of the correct value of 7.0711. Seeing an integer result of taking the square root appears to indicate that 50 is a perfect square! To avoid this error, root is data type float  that is, decimal points are permitted. root = sqrt(s). This line takes the square root of argument s and stores it in variabe root. Any decimal points resulting are retained. if root = int(root). This line can be paraphrased as "if the square root of s is equal in value to the square root of s with its decimal part chopped off ..." Going back to the example of 50, root is 7.0711 and int(root) is 7. They are of different values. The code would decide that 50 isn't a square. Try the s value of 49. The value in root is 7.0 complete with decimal point. int(root) = 7 with no decimal point. However, their actual values are the same. So the code would decide that, for an s value of 49, we have a perfect square. When this occurs, the line return 1 is executed, which means "return TRUE to the calling routine." If s isn't a perfect square, the line return 0 is executed, meaning "return FALSE to the calling routine." 
MAIN MENU
HOW DOES IT WORK?
Site design/maintenance: Dave Ellis Email me!
Last Updated: March 27th, 2005.