QBasic / QB64 Discussion Forum      Other Subforums, Links and Downloads
 Return to Index  

Here's a short way. . .

July 22 2005 at 10:03 AM
rpgfan3233  (Login rpgfan3233)


Response to Why are my equal numbers not comparing as equal?

A short way to make sure that they are equal is to store the value of the number raised to a power in a variable.
=======
FOR j = 1 TO 20
  variab = j ^ 2
  IF (variab) <> (j * j) THEN PRINT j; 'nothing should be PRINTed
NEXT j
=======

This works because the value is calculated before comparison. The original statement that the exponentiation operator (^) doesn't work properly in IF statements is still true however. To prove this, here is a program that will show the values:
=======
PRINT "j", "j ^ 2", "j * j"
FOR j = 1 TO 20
  PRINT j, j ^ 2, j * j
NEXT j
=======

This proves that j^2 and j*j are equal to each other in the end.

 
 Respond to this message