Re: if statement / isinteger

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Fri Jan 28 2011 - 00:00:40 MST

Paula,

I think you want to use round(x,3) to get an integer result, not
round(x,0). See doc page for the round function.

Please check your data types at *every line* when you run into this
kind of problem, it makes debugging a lot easier. Use print()
liberally. Edited demo:

ncl 0> x=12.3
ncl 1> print (x)
Variable: x
Type: float
Total Size: 4 bytes
             1 values
(0) 12.3

ncl 2> xround = round(x,0)
ncl 3> print (xround)
Variable: xround
Type: float <---------- FLOAT
Total Size: 4 bytes
             1 values
(0) 12

ncl 4> xround2 = round(x,3)
ncl 5> print (xround2)
Variable: xround2
Type: integer <---------- INTEGER
Total Size: 4 bytes
             1 values
(0) 12

Now that you have integer data, I think you want to decide between
even and odd integers. The isinteger function will not help; it
looks at only the data type of a variable, not its value. You
already have the integer divide by 2 method, just use it like this:

ncl 7> b = 12
ncl 8> c = (b / 2) * 2 ; only works for integers
ncl 9> print (c)
Variable: c
Type: integer
(0) 12

ncl 10> b = 13
ncl 11> c = (b / 2) * 2 ; only works for integers
ncl 12> print (c)
Variable: c
Type: integer
(0) 12

In an if statement, all you need to do is this:

    if (b .eq. c) then ; only works for integers
       * EVEN *
    else
       * ODD *
    end if

Alternate method with the mod function, no extra variable:

    if ((b % 2) .eq. 0) then ; only works for integers
       * EVEN *
    else
       * ODD *
    end if

--Dave

On 1/27/2011 8:37 PM, Paula Doubrawa Moreira wrote:
> Sorry the *do* just entered in the email, I don't actually have it
> in the script. My problem is that it's returning False, not True...
>
> I want *x* to be always an odd whole number, so say *x=12.3*
>
> *xround = round(x,0) *will be 12
> *a = xround/2 *will be 6, except even though it's a whole
> number, it's still not an integer, it's a "float" like *x* was
> originally.
>
> So when I test with *isinteger* it gives me False and doesn't
> execute the statement. Intuitively I though the number would
> "convert itself" to integer if it was a whole number but of course
> it stays what it is. I just wanted to add 1 if my number was even,
> because I only wanna work with odds.
>
> Any easier way to sort between odds/evens?
>
> Thanks!
>
>
> On Thu, Jan 27, 2011 at 6:23 PM, Dennis Shea <shea@ucar.edu
> <mailto:shea@ucar.edu>> wrote:
>
> The function returns a logical value [True / False]
>
> function isinteger (
> arg
> )
> return_val [1] : logical <====
>
> Usage
>
> if ( isinteger(a) ) then ; <<< This is if (True) then
>
> a=a+1
> end if
>
> Why do you have "do a=a+1"
> Where did that come from
>
>
>
>
> On 1/27/11 8:13 PM, Paula Doubrawa Moreira wrote:
>
> Hi-
> Can anyone help me use if statements and logical structures
> together?
>
> how do I use the function "isinteger" with an if statement?
>
> I've tried:
>
> *if ( isinteger(a) ) then*
> *do a=a+1*
> *end if*
>
> *if ( isinteger(a) .eq. "True") then*
> *do a=a+1*
> *end if*
> *
> *
> I've tried moving things around in the if statement and even
> though I
> know that a is an integer, it's not recognizing my if
> statement and
> executing the command!
>
> Thanks!
>
> --
> *Paula Doubrawa Moreira*/
> /Graduate Student - Research Assistant
> International Arctic Research Center
> University of Alaska Fairbanks
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Jan 28 00:00:47 2011

This archive was generated by hypermail 2.1.8 : Mon Jan 31 2011 - 10:38:24 MST