Logical operators [message #93867] |
Sun, 13 November 2016 06:30  |
Gompie
Messages: 76 Registered: August 2012
|
Member |
|
|
Hi
I am trying to do
if( ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50)) then begin
c=! NANQ
...but the multiplie logical operator not working.
Can you help
GlanPlon
|
|
|
|
Re: Logical operators [message #93869 is a reply to message #93868] |
Sun, 13 November 2016 08:18   |
Gompie
Messages: 76 Registered: August 2012
|
Member |
|
|
Thanks
By not working, I mean the conditions are not applied and I get C=!NANQ everywhere.
Can logical operators be combined in idl or not.
Glan
On Sunday, November 13, 2016 at 10:40:46 AM UTC-5, Jim P wrote:
> On Sunday, November 13, 2016 at 7:30:50 AM UTC-7, Glan wrote:
>> Hi
>> I am trying to do
>> if( ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50)) then begin
>> c=! NANQ
>>
>> ...but the multiplie logical operator not working.
>> Can you help
>> GlanPlon
>
> You don't say what "not working" means, but my first guess is that the variables a and b are not both scalars. You may need to re-cast your problem using a vector operation, a WHERE or a loop.
>
> Jim P
|
|
|
Re: Logical operators [message #93870 is a reply to message #93869] |
Sun, 13 November 2016 08:39   |
Jim Pendleton
Messages: 165 Registered: November 2011
|
Senior Member |
|
|
On Sunday, November 13, 2016 at 9:18:27 AM UTC-7, Glan wrote:
> Thanks
> By not working, I mean the conditions are not applied and I get C=!NANQ everywhere.
> Can logical operators be combined in idl or not.
> Glan
>
> On Sunday, November 13, 2016 at 10:40:46 AM UTC-5, Jim P wrote:
>> On Sunday, November 13, 2016 at 7:30:50 AM UTC-7, Glan wrote:
>>> Hi
>>> I am trying to do
>>> if( ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50)) then begin
>>> c=! NANQ
>>>
>>> ...but the multiplie logical operator not working.
>>> Can you help
>>> GlanPlon
>>
>> You don't say what "not working" means, but my first guess is that the variables a and b are not both scalars. You may need to re-cast your problem using a vector operation, a WHERE or a loop.
>>
>> Jim P
My new guess is that A and B always satisfy the IF statement for your data. What combination of "a" and "b" is evaluated to TRUE that you expect to evaluate to FALSE?
Jim P
|
|
|
Re: Logical operators [message #93871 is a reply to message #93867] |
Sun, 13 November 2016 19:05  |
Phillip Bitzer
Messages: 223 Registered: June 2006
|
Senior Member |
|
|
On Sunday, November 13, 2016 at 8:30:50 AM UTC-6, Glan wrote:
> Hi
> I am trying to do
> if( ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50)) then begin
> c=! NANQ
>
> ...but the multiplie logical operator not working.
> Can you help
> GlanPlon
First, and just to clarify for others, "and" and "or" or not, strictly speaking, logical operators (&&, ||). They are bitwise. Here, they essentially work the same as the logical ops, since the result of the relational operators (lt,gt, etc.) lead to ones or zeros. But, the logical operators have the added benefit of "short-circuiting," so that 1 || _some_expression is true, without evaluating _some_expression. Similarly, 0 && _some_expression is false, without evaluating _some_expression.
With that said, a simple test shows the code does not always evaluate to true:
IDL> a = 0 & b=0
IDL> ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50))
0
IDL> a = 0 & b=51
IDL> ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50))
1
IDL> a = 51 & b=0
IDL> ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50))
1
IDL> a = 51 & b=51
IDL> ( (a gt 50) and ( b lt 50) ) or ((a lt 50) and ( b gt 50))
0
(Your code seems to be missing a parentheses, BTW.)
|
|
|