Applesoft: Numeric Comparison Issues

When two numbers print as equal, though an IF statement indicates they're not, the least significant bits in the internal binary storage format of those numbers are different.

Also, sometimes Applesoft's math package doesn't give the answer you expect because Applesoft calculations are done in a 32 bit binary floating point format, which have no exact equivalents to most numbers.
Applesoft's PRINT statement truncates a number that is extremely close to being an integer. For example, 3^2 and 3*3 will both print as 9 but won't compare as equal. Printing 3^2 - 3*3 will result in 3.7252903E09, while the expression 3^2 = 3*3 is false.

Also, Applesoft uses natural logarithms to calculate many of its trancendental functions, which adds small errors to the results.

To avoid comparison problems, round Applesoft real numbers to a specific number of decimal places.

Use the formula: X = INT(X*P+.5)/P

where P=10 for 1 decimal place, P=100 for 2 decimal places and P=1000 for 3 decimal places, etc.
Published Date: Feb 18, 2012