HELP with IF statement [message #91268] |
Thu, 25 June 2015 04:10  |
g.nacarts
Messages: 148 Registered: November 2013
|
Senior Member |
|
|
I have a problem with IF statement
I wanted to check whether my array A has any zero element and if so then simply return a zero array (with two zeroes). I typed the following but am not sure if it's correct.
A FLOAT = Array[58]
FOR i=0L, 57 DO BEGIN
IF A[i] EQ 0 THEN RETURN, [0,0]
ENDFOR
|
|
|
Re: HELP with IF statement [message #91271 is a reply to message #91268] |
Thu, 25 June 2015 05:22   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
g.nacarts@gmail.com writes:
> I have a problem with IF statement
>
> I wanted to check whether my array A has any zero element and if so then simply return a zero array (with two zeroes). I typed the following but am not sure if it's correct.
>
> A FLOAT = Array[58]
>
> FOR i=0L, 57 DO BEGIN
> IF A[i] EQ 0 THEN RETURN, [0,0]
> ENDFOR
There are a whole host of articles on my web page explaining why it will
be impossible to find a zero value in an array of floats. I'll just give
you the bottom line: use Floats_Equal from the Coyote Library to do this
kind of comparison:
IF Floats_Equal(a[i],0) THEN RETURN, [0,0]
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|
|
|
|
Re: HELP with IF statement [message #91277 is a reply to message #91271] |
Thu, 25 June 2015 08:20   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
On 06/25/15 08:22, David Fanning wrote:
> g.nacarts@gmail.com writes:
>
>> I have a problem with IF statement
>>
>> I wanted to check whether my array A has any zero element and if
>> so then simply return a zero array (with two zeroes). I typed the
>> following but am not sure if it's correct.
>>
>> A FLOAT = Array[58]
>>
>> FOR i=0L, 57 DO BEGIN
>> IF A[i] EQ 0 THEN RETURN, [0,0]
>> ENDFOR
>
> There are a whole host of articles on my web page explaining why it will
> be impossible to find a zero value in an array of floats. I'll just give
> you the bottom line: use Floats_Equal from the Coyote Library to do this
> kind of comparison:
>
> IF Floats_Equal(a[i],0) THEN RETURN, [0,0]
Yes. My first reply when I am asked this sort of question is: "What do
you think is zero?"
Or, if I'm feeling particularly obtuse: "What IS zero?" (with a suitable
gesticulatory flourish, of course :o)
Funny, now that I think about it, people haven't come to ask me
questions for a while now. Huh.
cheers,
paulv
|
|
|
|
|
Re: HELP with IF statement [message #91280 is a reply to message #91278] |
Thu, 25 June 2015 08:42   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
g.nacarts@gmail.com writes:
> Something that I don't understand. I tried to avoid the for loop created before and I replace this part of the code:
>
> for i=0,n-1 do begin
> IF floats_equal(A[i],0) THEN RETURN,[0,0]
> endfor
>
> By this:
>
> IF norm(A) EQ 0 THEN RETURN, [0,0]
>
> I thought I will end up with the same result but I didn't. Does anyone know why this happen?
Most of the people who follow this newsgroup knows why it happens:
http://www.idlcoyote.com/math_tips/sky_is_falling.html
http://www.idlcoyote.com/math_tips/storenum.html
http://www.idlcoyote.com/math_tips/razoredge.html
http://www.idlcoyote.com/code_tips/lhsvsrhs.html
http://www.idlcoyote.com/math_tips/double.html
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|
|
|