質問

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.)

役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top