Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
M Carmen (mcgonzal@uv.es) writes: > I have a question related with loops......can someone teel me why > > if i do a loop that goes to a maximum of 0.6: > for r=0.0, 0.6, 0.1 do begin & print, r & endfor > > I get: > 0.000000 > 0.100000 > [........] > 0.500000 > 0.600000 > > and if now i change the maximum value of the range to 0.7: > for r=0.0, 0.7, 0.1 do begin & print, r & endfor > > I get: > 0.000000 > 0.100000 > [........] > 0.500000 > 0.600000 > > EXACTLY THE SAME!!! Well, the short answer is "Because of the way computers represent floating point numbers." There have been numerous posts on this topic in the past. You might try searching the Google archives for "Set Precision", for example. The bottom line, however, is that it is not a good idea to use floating point values as counters, since you can't rely on their exact value. This has nothing to do with IDL. It is entirely related to how computers work. Cheers, David -- David W. Fanning, Ph.D. Fanning Software Consulting Phone: 970-221-0438, E-mail: david@dfanning.com Coyote's Guide to IDL Programming: http://www.dfanning.com/ Toll-Free IDL Book Orders: 1-888-461-0155
David Fanning <david@dfanning.com> writes: > M Carmen (mcgonzal@uv.es) writes: > >> I have a question related with loops......can someone teel me why >> >> if i do a loop that goes to a maximum of 0.6: >> for r=0.0, 0.6, 0.1 do begin & print, r & endfor >> ... >> >> EXACTLY THE SAME!!! > > Well, the short answer is "Because of the way computers > represent floating point numbers." There have been numerous > posts on this topic in the past. You might try searching > the Google archives for "Set Precision", for example. The > bottom line, however, is that it is not a good idea to use > floating point values as counters, since you can't rely on > their exact value. This has nothing to do with IDL. It is > entirely related to how computers work. And to prove David's point, try this little example: IDL> print, 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 - 0.7 5.96046e-08 The quantities 0.1 and 0.7 can't be represented exactly in a floating point, so there will inevitably be some truncation errors. Going to double precision doesn't always help (though it does here). Craig -- ------------------------------------------------------------ -------------- Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response ------------------------------------------------------------ --------------
M Carmen wrote: > Hello, > > I have a question related with loops......can someone teel me why > > if i do a loop that goes to a maximum of 0.6: > for r=0.0, 0.6, 0.1 do begin & print, r & endfor > > I get: > 0.000000 > 0.100000 > [........] > 0.500000 > 0.600000 > > and if now i change the maximum value of the range to 0.7: > for r=0.0, 0.7, 0.1 do begin & print, r & endfor > > I get: > 0.000000 > 0.100000 > [........] > 0.500000 > 0.600000 > > EXACTLY THE SAME!!! > > I have IDL Version 5.4 > > Thanks :) ! > M Carmen Hi M Carmen, I asked this very question not so long ago on this newsgroup (probably around October/November last year) - you might want to check out that thread. Basically the answer was "never use a float as the loop variable." Also, Jeff Hester probably has some helpful :-) comments to pass on. I know I was particularly encouraged and assisted by his replies, Andre