comp.lang.idl-pvwave archive - RDF feed
type: channel
description: Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
items: Re: crazy loops

Re: crazy loops
type: item
description: 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
creator: David Fanning
date: 2002-02-13T13:35:36-00:00

Re: crazy loops
type: item
description: 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 ------------------------------------------------------------ --------------
creator: Craig Markwardt
date: 2002-02-13T14:04:10-00:00

Re: crazy loops
type: item
description: 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 One should exercise great care when comparing the results of floating point calculations: (If these wrap around on your read, i apologize.) run these lines: IDL> limit = 0.7 & for r=0.0, limit, 0.1 do print, r,limit,r le limit,format='(f50.25,f50.25,f8.4)' 0.0000000000000000000000000 0.6999999880790710449218750 1.0000 0.1000000014901161193847656 0.6999999880790710449218750 1.0000 0.2000000029802322387695312 0.6999999880790710449218750 1.0000 0.3000000119209289550781250 0.6999999880790710449218750 1.0000 0.4000000059604644775390625 0.6999999880790710449218750 1.0000 0.5000000000000000000000000 0.6999999880790710449218750 1.0000 0.6000000238418579101562500 0.6999999880790710449218750 1.0000 IDL> limit = 0.6 & for r=0.0, limit, 0.1 do print, r,limit,r le limit,format='(f50.25,f50.25,f8.4)' 0.0000000000000000000000000 0.6000000238418579101562500 1.0000 0.1000000014901161193847656 0.6000000238418579101562500 1.0000 0.2000000029802322387695312 0.6000000238418579101562500 1.0000 0.3000000119209289550781250 0.6000000238418579101562500 1.0000 0.4000000059604644775390625 0.6000000238418579101562500 1.0000 0.5000000000000000000000000 0.6000000238418579101562500 1.0000 0.6000000238418579101562500 0.6000000238418579101562500 1.0000 The for loop checks r le upper limit, and then executes if true. Here, 0.6 is actually a little gt 0.600000000000, and 0.7 is just less than 0.700000000000000000000000000. Hence the seemingly goofy results. Cheers, bob PS to explain why …
creator: Robert Stockwell
date: 2002-02-13T17:14:03-00:00

Re: crazy loops
type: item
description: 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
creator: Andre Kyme
date: 2002-02-13T22:11:46-00:00