Page 2 of 2

Re: Math error in simple FOR loop with decimal 0.01

Posted: Thu Mar 16, 2023 8:21 pm
by kelli217
BTW, here's a fun one:

10 FOR X=10 TO 0 STEP -0.1:NEXT X:PRINT X

Re: Math error in simple FOR loop with decimal 0.01

Posted: Thu Mar 16, 2023 9:08 pm
by BruceRMcF
TomXP411 wrote: Thu Mar 16, 2023 7:19 pm ... (As was pointed out above, the Commodore packed format has a 31-bit mantissa. But this doesn't change the nature of irrational numbers, it just makes them longer.) ...
Quite. It takes more iterations to hit the same imprecision as with 32bit floats, but it will still get there, since there will always be a rounding error at the last bit.

Re: Math error in simple FOR loop with decimal 0.01

Posted: Thu Mar 16, 2023 11:49 pm
by EbonHawk
Ran the same 3 sets in C64 BASIC (VICE emulator) and the first program hits the same exact issue at the same number (23rd), but doesn't hit the rounding problem till the 79th iteration with the 2nd setup.

Just weird.

But anyway, glad to know it's at least known.