# Why are my equal numbers not comparing as equal?

April 15 2004 at 7:51 AM

FAQ024 = Why are equal numbers not comparing as equal?

There are cases when two expressions that should be exactly equal are not treated as equal by QBasic. This is usually the result of conversions from decimal to internal floating point that cannot be overcome by the programmer. It is a feature of QBasic.

Mac

==========================================================
Run the following program:
DIM j AS DOUBLE
j = .1: PRINT j
j = 1 / 10: PRINT j

You will get
.1000000014901161
.1

Conclusion: Decimals (.1, etc.) do not always accurately work.

Workaround: You have to round off the result or otherwise ignore the error. For example instead of
IF j = .1 THEN
you would have to code
IF ABS(J - .1) < .000001
(or something else that accomplishes that)

==========================================================
Run the following program:
FOR j = 1 TO 20
IF (j ^ 2) <> (j * j) THEN PRINT j;
NEXT j

You will get
5  7  9  10  11  13  14  15  17  18  19  20

Conclusion: The power operator (^) does not work accurately in IF expressions.

Workaround: Instead of (j^2), use (j*j)

==========================================================

 This message has been edited by Mac36 on Apr 15, 2004 7:57 AM

 Respond to this message
Responses