Re: crazy loops [message #29338] |
Wed, 13 February 2002 14:11 |
Andre Kyme
Messages: 19 Registered: September 2001
|
Junior Member |
|
|
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
|
|
|
Re: crazy loops [message #29344 is a reply to message #29338] |
Wed, 13 February 2002 09:14  |
Robert Stockwell
Messages: 74 Registered: October 2001
|
Member |
|
|
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 you do not see the limit = 0.7 case, for that iteration
r = 0.7099999785423278808593750 and the limit is 0.6999999880790710449218750.
|
|
|
Re: crazy loops [message #29354 is a reply to message #29344] |
Wed, 13 February 2002 06:04  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
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
------------------------------------------------------------ --------------
|
|
|
Re: crazy loops [message #29359 is a reply to message #29354] |
Wed, 13 February 2002 05:35  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
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
|
|
|