Search code examples
idl-programming-languageenvi

Why IDL have different value in same expression?


I'm doing some image processing with IDL, and it required a high-precision. But when I debug my colleague's program, I find some thing strange:

IDL> lat,y_res
   45.749001
   0.00026999999
IDL> lat - findgen(10)*y_res + y_res * 0.5 + findgen(10)*y_res + y_res * 0.5
   45.749268       45.749268       45.749268       45.749268 ...      45.749268
IDL> lat - (findgen(10)*y_res + y_res * 0.5) + (findgen(10)*y_res + y_res * 0.5)
   45.749001       45.749001       45.749001       45.749001 ...

Just as code above, I don't know why the two results have different value? My IDL version is 8.3 with ENVI package.


Solution

  • TriskalJM is correct. If you look at your parentheses in the second expression, you are grouping your terms differently. This will always happen with floating-point arithmetic in any computer language, just due to roundoff errors. If you want a more information, you could consult: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

    In the meantime, I would recommend that you switch to double-precision:

    lat - dindgen(10)*y_res + y_res * 0.5 + dindgen(10)*y_res + y_res * 0.5