Forum Migration Notice
Update (2026-01-21): The user forums are now in read-only mode pending the data migration.

Update (2026-01-12): The user forums will be put into read-only mode on the 21st of January, 00:00 CET, to prepare for the data migration.

We're transitioning to a more modern community platform by beginning of next year. Learn about the upcoming changes and what to expect.

Bug in arithmetic

Hi, I wrote this code, ran it in the real controller and I was really surprised.

VAR num ii

VAR num AngleStep:=0.1

FOR i FROM 0 TO 360 STEP AngleStep DO

ii:=ii + AngleStep

ENDFOR

result is ... ii = 359.918

Comments


  • Hi Martiner.

    In Microsoft Visual Basic, with Single precision variables, the result is 360.0128
    Probably the FOR statement is implemented in a slightly different way, causing an iteration less, so your error is 0.018; more or less ...
    But from a theorical point of view your code is very poor. Using floating point numbers always introduce an error. Iterating a sum 3600 times will multiply the error by 3600. Better is:

    FOR i FROM 0 TO 3600 DO
      ii :=  i / 10
      !
      ! I think you do something here
      !
    ENDWHILE

    Regards
    Claudio

    claudio2007-2-1 18:17:36