0

I recently came across this apparent inconsistency when going through someone's IDL code:

IDL> for i=0.,1.9,1. do print,i
      0.00000
      1.00000
IDL> for i=0.,1.9,1 do print,i
      0.00000
      1.00000
IDL> for i=0.,1.9 do print,i
      0.00000
      1.00000
      2.00000

Could anyone explain if there is a logic behind the behaviour in the third loop.

The IDL version used is 8.2.0.

(Note that my own code would stick to using integers in a for loop, and definitely after finding this.)

2 Answers2

0

This definitely has to do with floating point accuracy. What is recommended in IDL is not create explicit floating point loops, but use an integer loop where in the body the floating point value is derived from.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Sorry, I don't see how floating point accuracy would explain this. Because then either the first or the second loop should show the same behaviour, since the increment is implicitly set to 1 (and promoted to float). –  Mar 05 '13 at 09:23
  • Some googling around suggested this as the most likely suspect. – Paul Hiemstra Mar 05 '13 at 09:32
0

This might be a bug in IDL 8.2 - when I run the commands in version 8.2.1 I get the same behaviour as you. When I run them on a different computer with version 8.0 I get the expected behaviour, i.e. each command gives the same output. The inbuilt IDL help certainly suggests that they should all give the same output.

yamez
  • 108
  • 6
  • I finally put it on the IDL Exelis forums, and while the [answer](http://www.exelisvis.com/Support/Forums/tabid/184/forumid/7/threadid/14243/scope/posts/Default.aspx) is somewhat disappointing, it looks like it can be confirmed as a bug. Thanks for cross-checking and hinting about the version check. –  Apr 24 '13 at 14:08