fastbasic icon indicating copy to clipboard operation
fastbasic copied to clipboard

Enhancement request: support floating point variables in FOR

Open marianodominguez opened this issue 2 years ago • 2 comments

things like

for th%=-PI% to 2*PI% step PI%/20

should be possible to implement, this will avoid while / loop refactors

maybe another imp if the loop variable is float.

marianodominguez avatar Apr 06 '23 05:04 marianodominguez

Hi!

Sorry, I won't implement this, as FOR loops with floating point variables are a bad idea:

  • It is a lot slower, as you have an extra floating-point comparison on each step.
  • Is nor predictable, as floating-point math is not exact. In your example above you will loop 59, 60 or 61 times depending on the floating-point implementation and rounding.

Correct way to implement above loop is:

th%=-PI%
FOR i=0 TO 60
   '..... do something...
   th% = th% + PI%/20
NEXT

Have Fun!

dmsc avatar Apr 06 '23 13:04 dmsc

That makes sense. still wrapping my head on mixing integer and float variables. thanks !

On Thu, Apr 6, 2023 at 6:41 AM dmsc @.***> wrote:

Hi!

Sorry, I won't implement this, as FOR loops with floating point variables are a bad idea:

  • It is s lot slower, as you have an extra floating-point comparison on each step.
  • Is nor predictable, as floating-point math is not exact. In your example above you will loop 59, 60 or 61 times depending on the floating-point implementation and rounding.

Correct way to implement above loop is:

th%=-PI% FOR i=0 TO 60 '..... do something... th% = th% + PI%/20 NEXT

Have Fun!

— Reply to this email directly, view it on GitHub https://github.com/dmsc/fastbasic/issues/71#issuecomment-1499083745, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOXJI6N7CYP24ULJ7KFK53W73BYFANCNFSM6AAAAAAWU53E4Y . You are receiving this because you authored the thread.Message ID: @.***>

--


Mariano Domínguez Molina


marianodominguez avatar Apr 06 '23 16:04 marianodominguez