Page 1 of 1

Use decimal number increment for the code DO LOOP

Posted: Fri Nov 08, 2024 12:17 pm
by Brc
I need to use Do loop with increment 0.1

Let's say that the variable X is defined between 1 and 11 with increment 0.1.
DO X = 1,11,0.1

....

END DO X


When I run this code, RATS gives error on "usage of real integer number" as an increment.

How can I use a decimal number as an increment in DO LOOP?

Thank you for your helps.

Re: Use decimal number increment for the code DO LOOP

Posted: Fri Nov 08, 2024 1:40 pm
by TomDoan
You can't use DO with non-integers. Use the more general DOFOR:

dofor [real] x = %SEQRANGE(1,11,101)
   ?x
end dofor

Note that you really can't just ask to go from 1 to 11 by steps of .1 with machine arithmetic (which is why %SEQRANGE uses a count rather than an increment). Because .1 isn't represented exactly in binary, adding .1 100 times might or might not end up hitting 11 exactly. It will round to 11, but might actually be something like 10.99999999999.