comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Strange problem
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Strange problem [message #28141] Sun, 25 November 2001 16:46 Go to next message
Andre Kyme is currently offline  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 #28206 is a reply to message #28141] Mon, 26 November 2001 14:16 Go to previous messageGo to next message
Paul van Delst is currently offline  Paul van Delst
Messages: 364
Registered: March 1997
Senior Member
Andre Kyme wrote:
>
> "Pavel A. Romashkin" wrote:
>
>> Please enlighten me why would one want to use a floating point value as
>> a counter?
>>
>> Pavel
>>
>> Andre Kyme wrote:
>>>
>>> Hi everyone,
>>>
>>> type: for i=0., 0.8., 0.1 do print,i
>>> That's bad isn't it?i
>
> Pavel,
> Floating point counters are so roomy you can put you're PC, lunch, coffee,
> and notes on them and still have room to move!

Apparently they're not so roomy as to avoid spilling your coffee all over your PC and using
your notes as lunch napkins.

paulv

--
Paul van Delst Religious and cultural
CIMSS @ NOAA/NCEP purity is a fundamentalist
Ph: (301)763-8000 x7274 fantasy
Fax:(301)763-8545 V.S.Naipaul
Re: Strange problem [message #28220 is a reply to message #28141] Mon, 26 November 2001 10:03 Go to previous messageGo to next message
James Kuyper Jr. is currently offline  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.
[Offtopic] Re: Strange problem [message #28394 is a reply to message #28141] Wed, 05 December 2001 16:24 Go to previous messageGo to next message
Bhautik Joshi is currently offline  Bhautik Joshi
Messages: 21
Registered: November 2001
Junior Member
> 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 |
\---------------------------/
Re: [Offtopic] Re: Strange problem [message #28505 is a reply to message #28394] Fri, 07 December 2001 08:29 Go to previous message
Jeff Hester is currently offline  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 Go to previous message
Bhautik Jitendra Josh is currently offline  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 Go to previous message
Jeff Hester is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: ThreeJ Symbol
Next Topic: Research Systems Platform Support Update

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 10:58:50 PDT 2025

Total time taken to generate the page: 0.00481 seconds