RobotStudio event

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