[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: need help with cpu problem
The problem you describe is not a malfunction.
It is simply the way floating point arithmetic works.
When the computer calculates X ^ Y (assuming X=7 and Y=3), the
result is not exactly 343,
but instead is a number very close to (but not equal to) 343.
This happens because the easiest way to raise an arbitrary number
(i.e. the computer is not restricting the numbers to be integers)
is to take the log of X, multiply this by Y, and exponentiate to
get the result. Roundoff error causes the result to not exactly be
the integer result that you expect.
To demonstrate what happened, change line 40 to:
40 PRINT NUM - 343
Any time you do any kind of calculation, expect that roundoff error
could occur. Because of this, one should never test whether a floating
point number (resulting from a calculation) is equal to some other
floating point number. Instead, check if it is within a range.
For example, you could change line 40 to:
40 IF ABS (NUM - 343) < 1E-5 THEN PRINT NUM
I picked 1E-5 because Applesoft floating point numbers have about
9 digits of precision, so this lets us work with numbers up to 1000,
allowing the rightmost digit to be trashed by roundoff error and
still have the comparison work properly. The number of digits that
roundoff error will affect depends on the complexity of the calculations
that you do and how well the floating point math is implemented. I
have never seen it affect more than two digits except when the
calculation itself had other inherent problems that needed fixing.
The only time it is OK to compare two floating-point numbers for
equality is when neither of them have had any calculations done,
such as comparing a number input from the keyboard or a file to
a constant or some other number input from the keyboard or a file.
These concepts apply no matter what computer system or language
you are using.
Kevin Scott
scott@disney.kodak.com