Re: short circuit evaluation in array logical operations

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Fri, 2 Oct 2009 09:19:17 -0600

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
Received on Fri Oct 02 2009 - 09:19:17 MDT

This archive was generated by hypermail 2.2.0 : Mon Oct 05 2009 - 13:28:34 MDT