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