Strange problem [message #28141] |
Sun, 25 November 2001 16:46  |
Andre Kyme
Messages: 19 Registered: September 2001
|
Junior Member |
|
|
Hi everyone,
type: for i=0., 0.8., 0.1 do print,i
That's bad isn't it?
Or what about:
for i=0., 9.6, 0.1 do print,i
for i=0., 9.7, 0.1 do print,i
Same last element?
If you stick a "d" after the first 0 - ie. make it double precision:
for i=0.d, 0.8, 0.1 do print,i
- then it seems to be OK, kind of
Or try this:
a=fltarr(100)
for i=0., 9.7., 0.1 do a[i*10]=i
Oh darn, 2.3 is not there!
Neither is 9.3.
Using the "d" trick seems to fix it, but why the need when we're only
using steps of 0.1?
Anybody know what's going on?
Andre Kyme
Department of Medical Physics
Westmead Hospital
|
|
|
|
Re: Strange problem [message #28220 is a reply to message #28141] |
Mon, 26 November 2001 10:03   |
James Kuyper Jr.
Messages: 10 Registered: November 2001
|
Junior Member |
|
|
Andre Kyme wrote:
> Hi everyone,
>
> type: for i=0., 0.8., 0.1 do print,i
> That's bad isn't it?
You can get a better idea of what's going wrong by modifying that as
follows:
for i=0., 0.8., 0.1 do print,i,format='(f20.10)'
There's a warning about this in the User's Guide, in the part describing
the FOR statement.
> Or what about:
>
> for i=0., 9.6, 0.1 do print,i
> for i=0., 9.7, 0.1 do print,i
> Same last element?
>
> If you stick a "d" after the first 0 - ie. make it double precision:
>
> for i=0.d, 0.8, 0.1 do print,i
>
> - then it seems to be OK, kind of
>
> Or try this:
> a=fltarr(100)
> for i=0., 9.7., 0.1 do a[i*10]=i
> Oh darn, 2.3 is not there!
> Neither is 9.3.
> Using the "d" trick seems to fix it, but why the need when we're only
> using steps of 0.1?
>
> Anybody know what's going on?
Yes. You should never use floating point values as your loop variable;
there's only a finite set of numbers that can be represented exactly in
floating point notation; all other numbers are stored as the nearest
approximation from that set. Most decimal fractions can't be represented
exactly. None of the numbers you used except 0.0 can be represented
exactly. As a float, 0.1 is actually represented by approximately
0.100000001490, and 0.8 is represented by approximately 0.800000011921.
Therefore, you can't guarantee exactly how many times your loop will
execute. Also you get roundoff errors which accumulate every time you
increment the counter.
My preferrred solution is as follows:
for i=0, 8 do print,i*0.1
for i=0, 97 do a[i] = i*0.1
The integer increments are exact, so the number of values is guaranteed
to be correct. You only get 1 floating point roundoff error, from the
multiplication, so the numbers printed are more accurate.
|
|
|
|
Re: [Offtopic] Re: Strange problem [message #28505 is a reply to message #28394] |
Fri, 07 December 2001 08:29  |
Jeff Hester
Messages: 21 Registered: December 2001
|
Junior Member |
|
|
Bhautik,
I will respond in kind. I agree that IDL is a very useful and accessible tool.
I grew up thinking that "Image processing system" meant a PDP 11/55 running
Unix V6, a Genisco 512 X 512 x 8-bit image display that could be programmed
across a DEUNA parallel interface, and a C compiler. I cannot begin to tell
you the level of appreciation that I have for what IDL offers.
I have also worked with quite a few students over the years, and know two
things:
1. There is a certain base of knowledge that is necessary as a starting point
for any really interesting or useful discussion; and
2. When it comes to the basics of numerical computing, there are far more
efficient ways to obtain that knowledge than by trial and error.
If the fact that 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1 does not equal 0.8 seems
subtle or comes as a surprise, then there are many things that you do not know
but that you NEED to know that are much more fundamental than how to use IDL.
And as you try to learn IDL, you will find yourself tripping over your lack of
that basic knowledge again and again and again.
I do not know your position, the kind of work that you do, or anything else
about you. But I do know this. Your time would be very well spent if you went
off and either took a one semester computing course at a local college, or at
least picked up a text for such a course and worked through it yourself. You
would be very well served by the insight that you gain into what the computer
is doing when you add floating point numbers, for example. You also would
discover a whole suite of issues that will end up biting you in the ass if you
do not know they are coming. You might even gain some sensitivity to issues of
structured programming that will make your life far easier in the long run.
An IDL newsgroup is a reasonable place to begin to stumble through this
particular language. It is arguably a less reasonable place to learn to count.
Best wishes,
Jeff Hester
Bhautik Jitendra Joshi wrote:
>> Sorry, but I've really needed a good laugh for a while, and this thread has
>> provided it. It has even made for some amusing lunch-time conversation.
>> But all good things must come to an end. Besides, pulling the wings off
>> flies is not sporting. I should be ashamed.
>
> fly: 0o0 (fly is extremely happy)
>
> fly w/o wings: 0 'o' 0 (the little ' stand for little fly tears.
> the little fly is depressed because he
> (or she, lets not discriminate) has no
> more wings, and feels that they no
> longer serve any meaningful purpose)
>
> Mr. Hester - in all seriousness (really, I've got my serious face on now,
> look, I'm not even smirking), IDL is a useful, and more importantly,
> accessible tool that enables people from all walks of life, even those
> without a computer science background, to use computers for scientific
> data analysis.
>
> However, there are many users that still need to learn many things about
> scientific computing and am I not wrong in thinking that this is the
> perfect place to do it? This newsgroup is about (as far as I can tell)
> learning how to use IDL and perform scientific computing, and not to
> villify those who do not know it.
>
> ________________________________________________________
> / \
> |bjoshi@geocities.com | phone: 0404032617 |
> |ICQ #: 2464537 | http://i.am/captinnod |
> \~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(__)~~~~~~~/
> (__) |..|--\
> /--|^^| moo | |--|
> |--| | \OO/||^
> ^||\oo/ moo
--
Jeff Hester
Professor
Dept. of Physics & Astronomy
Arizona State University
jhester@asu.edu
|
|
|
Re: [Offtopic] Re: Strange problem [message #28507 is a reply to message #28394] |
Fri, 07 December 2001 06:28  |
Bhautik Jitendra Josh
Messages: 3 Registered: November 2001
|
Junior Member |
|
|
> Sorry, but I've really needed a good laugh for a while, and this thread has
> provided it. It has even made for some amusing lunch-time conversation.
> But all good things must come to an end. Besides, pulling the wings off
> flies is not sporting. I should be ashamed.
fly: 0o0 (fly is extremely happy)
fly w/o wings: 0 'o' 0 (the little ' stand for little fly tears.
the little fly is depressed because he
(or she, lets not discriminate) has no
more wings, and feels that they no
longer serve any meaningful purpose)
Mr. Hester - in all seriousness (really, I've got my serious face on now,
look, I'm not even smirking), IDL is a useful, and more importantly,
accessible tool that enables people from all walks of life, even those
without a computer science background, to use computers for scientific
data analysis.
However, there are many users that still need to learn many things about
scientific computing and am I not wrong in thinking that this is the
perfect place to do it? This newsgroup is about (as far as I can tell)
learning how to use IDL and perform scientific computing, and not to
villify those who do not know it.
________________________________________________________
/ \
|bjoshi@geocities.com | phone: 0404032617 |
|ICQ #: 2464537 | http://i.am/captinnod |
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(__)~~~~~~~/
(__) |..|--\
/--|^^| moo | |--|
|--| | \OO/||^
^||\oo/ moo
|
|
|
Re: [Offtopic] Re: Strange problem [message #28512 is a reply to message #28394] |
Thu, 06 December 2001 23:57  |
Jeff Hester
Messages: 21 Registered: December 2001
|
Junior Member |
|
|
And if you thought that was fun, try the following on for size:
IDL> print,32767+1
The amusing thing is that without even knowing it, your response proves my
point.
We could go on to a discussion of numerical diffusion if somebody wants to
start up a counter (integer, please - 32 bit unsigned) of the number of
brains that explode.
Sorry, but I've really needed a good laugh for a while, and this thread has
provided it. It has even made for some amusing lunch-time conversation.
But all good things must come to an end. Besides, pulling the wings off
flies is not sporting. I should be ashamed.
In all seriousness, if you don't understand why this issue is entirely about
the precision of floating point numbers -- if the problem is not completely
obvious to you -- and if you are trying to actually use IDL to do anything
that anyone will ever care about, however vaguely, then you really should go
and take a basic computing class or two. You are in serious danger of
producing garbage without even being able to recognizing it as such, or
having the slightest clue where it came from. You would not be the first to
announce to the world that you had "discovered" something that turned out to
be nothing more than your own ignorance of the pitfalls of numerical
computing.
My comment about IDL was also serious. IDL is an extraordinarily powerful
tool. I could not easily do what I do without it. But at the same time, it
is a loaded weapon left in an unlocked cabinet.
I'm not sure whether the next line should be, "C'est la vie," "C'est la
guerre," or "O caveat emptor."
Or maybe: you forth love if honk then
(Evidence of a misspent youth.)
Bhautik Joshi wrote:
>> The danger of IDL is that it allows people access to tools about which
>> they have no knowledge.
>> If the fact that floating point representations of numbers have limited
>> precision comes as a
>> shock, one can only wonder...
> As far as I can tell the issue wasn't about limited prescision. Unless
> its a fractional representation, *any* number is going to have a limited
> degree of prescision (whether thats on paper or in the computer) - thats
> something you learn in high school.
>
> The subtle issue of a floating point counter confusing the inequality
> operator in a loop isn't something thats obvious to everybody.
>
>> Is there an emoticon for "shudder in abject fear"?
> No. However, there is an emoticon for when drink international roast
> coffee.
>
> :E
>
> it demonstrates the process of your mouth disentegrating in abject
> objection to the awful aroma that can only be INTERNATIONAL ROAST.
>
> --
> /--------------------------------------------------(__)----- ----\
> | nbj@imag.wsahs.nsw.gov.au | phone: 0404032617 |..|--\ -moo |
> | ICQ #: 2464537 | http://cow.mooh.org | |--| |
> |---------------------------+----------------------\OO/|| ------/
> | international |
> | roast. my sanity has gone |
> | its lost forever |
> \---------------------------/
--
Jeff Hester
Professor
Dept. of Physics & Astronomy
Arizona State University
jhester@asu.edu
|
|
|