# Re: short circuit evaluation in array logical operations

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Fri, 2 Oct 2009 13:23:02 -0600

Hi Slava,
I would like to add to Wei's comments here because this issue was
brought up earlier this year by Dave Allured. I spent some time
studying the issue and I implemented a partial solution that does fix
the case you are concerned with:

print( (.not.ismissing(a)) .and. (a.le.0) )

such that it will return binary results (for your example) of True,
False, False.

However, I have hesitated to check in the fix because it does not
fully solve the problem, as explained in the message exchange that I
am attaching here. However, it is likely that a partial solution is
better than none, so probably it will be included in the next version
of NCL. And assuming the NCL development teams agrees, I can make it
available sooner if you like.
-dave

attached mail follows:

('binary' encoding is not supported, stored as-is)

On Oct 2, 2009, at 9:19 AM, Wei Huang wrote:

> Slava,
>
> NCL is for data analysis, and visualization.
> It is certainly different from other common language.
>
> With your example, we have :
> a = new(3, integer)
> a = (/-1, -999, 1/)
>
> Where a(1) is missing.
> So, when we want to do something with a,
> we may want to do something like:
>
> if(a = 0)
> do first thing, say, mark white
>
> if(a > 0)
> do second thing, say mark red
>
> if(a < 0)
> do third thing, say mark blue
>
> That is what most common language do.
>
> But here, in NCL, a, can have another situation, its value is missing.
> We need to distinguish this one from the above. Say here, we mark it
> black.
>
> Certainly people can argue that they can always check if that value is
> missing
> before doing anything, but they have to do:
>
> if(a < 0 .and. a != -999)
> mark blue
>
> But here is NCL, you can safely say:
>
> if(a < 0)
> mark blue
>
> as -999 is missing, therefor with a = -999, (a < 0) returns Missing,
> not True.
>
> People can certainly have their own ideas/thoughts,
> we may not be able to change yours,
> but certainly, NCL is doing this with a reason.
>
> In your example, if you do:
> print(.not.ismissing(a))
> (0) True
> (1) False
> (2) True
>
> if you do:
> print(a .le. 0)
> (0) True
> (1) Missing
> (2) False
>
> So please pay attention to the ".and." operator in your expression.
>
>
> Regards,
>
> Wei Huang
> huangwei_at_ucar.edu
> VETS/CISL
> National Center for Atmospheric Research
> P.O. Box 3000 (1850 Table Mesa Dr.)
> Boulder, CO 80307-3000 USA
> (303) 497-8924
>
>
>
>
>
> On Oct 2, 2009, at 8:49 AM, Vladyslav Lyubartsev wrote:
>
>> Hello,
>>
>> It seems short circuit evaluation in logical operations does not
>> work for
>> arrays.
>> ncl 11> v = new(1,integer)
>> ncl 12> v = -999
>> ncl 13> print( (.not.ismissing(v)) .and. (v.le.0) )
>> (0) False
>> ncl 14> a = new(3,integer)
>> ncl 15> a = (/-1,-999,1/)
>> ncl 16> print( (.not.ismissing(a)) .and. (a.le.0) )
>> (0) True
>> (1) Missing
>> (2) False
>> b(1) is Missing, VERY inconvenient, workaround is necessary.
>>
>> Regards,
>> Slava
>>
>> P.S. I do not like missing logical values in NCL. It is not binary
>> logic,
>> but ternary logic, a lot of problems for people coming to NCL from
>> "common"
>> languages.
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk

Received on Fri Oct 02 2009 - 13:23:02 MDT

This archive was generated by hypermail 2.2.0 : Fri Oct 09 2009 - 08:29:22 MDT