Re: assignment inside boolean expression [message #20593] |
Tue, 11 July 2000 00:00 |
Jeffrey Jones
Messages: 5 Registered: July 2000
|
Junior Member |
|
|
In article <396A3790.43C8EB31@astro.psu.edu>, Patrick Broos
<patb@astro.psu.edu> wrote:
> I was wondering if it's common knowlege that one can put an IDL
> assignment inside
> a boolean expression (like in the C language). For example
>
> if (v = 0) then ... assigns v and does not execute the "then"
> statement, while
> if (v = 1) then ... assigns v and does execute the then.
>
> Just as in C I find this leads to really nasty bugs.
>
>
> --
> ============================================================ ========
> Patrick S. Broos, Systems Analyst/Programmer patb@astro.psu.edu
>
> Department of Astronomy and Astrophysics My office 814-863-7947
> Penn State University
> 525 Davey Lab FAX 814-863-8686
> University Park, PA 16802-6305
> http://www.astro.psu.edu Group office 863-9550
> ============================================================ ========
>
>
>
The analogy with C only goes so far, it seems.
The following generates a syntax error:
x = 0
while ((x = x+1) lt 10) do print, x
whereas,
x = 10.0
while (x = x - 1.0) do print, x
produces:
9.00000
8.00000
7.00000
6.00000
5.00000
4.00000
3.00000
2.00000
1.00000
--
Jeffrey Jones
Raytheon ITSS
Code 975
NASA/Goddard Space Flight Center
Greenbelt, MD 20771 USA
office: Bldg 33, Rm A428
phone: 301.614.5721
email: jeffrey.a.jones.1@gsfc.nasa.gov
--
Jeff
jajvj@erols.com
|
|
|
Re: assignment inside boolean expression [message #20599 is a reply to message #20593] |
Tue, 11 July 2000 00:00  |
Ben Tupper
Messages: 186 Registered: August 1999
|
Senior Member |
|
|
Craig Markwardt wrote:
> Ben Tupper <tupper@seadas.bigelow.org> writes:
>> "Liam E.Gumley" wrote:
>>
>>>
>>> Recall that in IDL, integers with odd non-zero values are Boolean
>>> 'True'. Beware of floats and doubles though, where any non-zero value is
>>> Boolean 'True'.
>>>
>>
>> Dang!
>>
>> I just spent an hour figuring out an efficient way of determining if an
>> integer is odd!
>>
>> I have an IDL function ISODD() for sale... very cheap. No reasonable
>> offer refused.
>
> Not to undercut you, but will (X AND 1) do the trick?
>
Thanks to Ken and Craig. I think for my purposes the following should suffice (I
guess as long as I make sure that I'm working with an integer/long/byte type.)
X = Indgen(6) - 2
For i = 0, N_elements(X)-1 Do $
If X[i] then Print, X[i], ': Odd' Else print, X[i], ': Even'
-2: Even
-1: Odd
0: Even
1: Odd
2: Even
3: Odd
Thanks again,
Ben
--
Ben Tupper
Bigelow Laboratory for Ocean Science
tupper@seadas.bigelow.org
pemaquidriver@tidewater.net
|
|
|
Re: assignment inside boolean expression [message #20600 is a reply to message #20593] |
Tue, 11 July 2000 00:00  |
promashkin
Messages: 169 Registered: December 1999
|
Senior Member |
|
|
I might add (X MOD 2) to the bidding...
Cheers,
Pavel
Ben Tupper wrote:
> Dang!
>
> I just spent an hour figuring out an efficient way of determining if an
> integer is odd!
>
> I have an IDL function ISODD() for sale... very cheap. No reasonable
> offer refused.
>
> Ben
|
|
|
Re: assignment inside boolean expression [message #20601 is a reply to message #20593] |
Tue, 11 July 2000 00:00  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Ben Tupper <tupper@seadas.bigelow.org> writes:
> "Liam E.Gumley" wrote:
>
>>
>> Recall that in IDL, integers with odd non-zero values are Boolean
>> 'True'. Beware of floats and doubles though, where any non-zero value is
>> Boolean 'True'.
>>
>
> Dang!
>
> I just spent an hour figuring out an efficient way of determining if an
> integer is odd!
>
> I have an IDL function ISODD() for sale... very cheap. No reasonable
> offer refused.
Not to undercut you, but will (X AND 1) do the trick?
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
|
|
Re: assignment inside boolean expression [message #20609 is a reply to message #20593] |
Mon, 10 July 2000 00:00  |
promashkin
Messages: 169 Registered: December 1999
|
Senior Member |
|
|
I can't see why is this unexpected. Anything in parenthesis gets
processed first, then evaluated. It looks the same as writing IF 1 THEN
... I can't see a source of bugs here, but it sure is possible to make
nested, parenthesized code that no other mind will be able to comprehend.
Cheers,
Pavel
Patrick Broos wrote:
>
> I was wondering if it's common knowlege that one can put an IDL
> assignment inside
> a boolean expression (like in the C language). For example
>
> if (v = 0) then ... assigns v and does not execute the "then"
> statement, while
> if (v = 1) then ... assigns v and does execute the then.
>
> Just as in C I find this leads to really nasty bugs..
|
|
|
|
Re: assignment inside boolean expression [message #20612 is a reply to message #20609] |
Mon, 10 July 2000 00:00  |
Liam E. Gumley
Messages: 378 Registered: January 2000
|
Senior Member |
|
|
Patrick Broos wrote:
> I was wondering if it's common knowlege that one can put an IDL
> assignment inside
> a boolean expression (like in the C language). For example
>
> if (v = 0) then ... assigns v and does not execute the "then"
> statement, while
> if (v = 1) then ... assigns v and does execute the then.
>
> Just as in C I find this leads to really nasty bugs.
Curious: I've never even considered using this syntax.
Enclosing a statement inside parentheses turns it into an expression,
which has a type and a value, e.g.
IDL> help, (v = 100)
<Expression> INT = 100
The variables in the right hand side of the statement must necessarily
be defined:
IDL> help, (zv = tv + vt)
% Variable is undefined: VT.
% Execution halted at: $MAIN$
If you take the following statements:
IDL> if (v = 0) then print, 'True'
IDL> help, v
V INT = 0
IDL> if (v = 1) then print, 'True'
True
IDL> help, v
V INT = 1
and remove the parentheses, the equivalent code is
IDL> v = 0
IDL> if (v) then print, 'True'
IDL> help, v
V INT = 0
IDL> v = 1
IDL> if (v) then print, 'True'
True
IDL> help, v
V INT = 1
Recall that in IDL, integers with odd non-zero values are Boolean
'True'. Beware of floats and doubles though, where any non-zero value is
Boolean 'True'.
Cheers,
Liam.
http://cimss.ssec.wisc.edu/~gumley
|
|
|