A set of math functions has been developed for use within the GrADS scripting language. Their use is somewhat self-explanatory, based on the following descriptions of the arguments and return codes.
else if (cmpwrd(name,"math_sqrt")) rc = gsfmath(pcmn,8);
else if (cmpwrd(name,"math_abs")) rc = gsfmath(pcmn,9);
rc = math_trigfunc(angle <,angle2> )
trigfunc sin, cos, tan, asin, acos, atan, atan2,
sinh, cosh, tanh, asinh, acosh, or atanhangle must be given in radians
angle2 (only used for atan2) rc the result of
the trig function calculation
rc = math_format(format,num)
format a C-language style format
statement for a floating point number, e.g. %5.2fnum the number
to be formattedrc the formatted
number
rc = math_nint(num)
num a real number
in decimal formrc num
rounded up or down to the nearest integer
rc = math_int(num)
num a real number
in decimal formrc the integer
part of num not greater than num
rc = math_log(num);
num any number
> 0rc natural logarithm
of num
rc = math_log10(num);
num any number
> 0.0rc base 10 logarithm of num
rc = math_pow(num,exponent);
num any number
exponent any number
rc num
raised to the power exponent
rc = math_sqrt(num)
num any numberrc the square root
of num
rc = math_abs(num)
num any numberrc the absolute
value of num
rc = math_exp(num)
num any numberrc the result of
the exponential function; e raised to the power num
rc = math_fmod(num1,num2);
num1 any numbernum2 any number not
equal to zerorc the remainder
when num1 is divided by num2
rc = math_mod(num1,num2);
num1 any numbernum2 any number not
equal to zerorc the integer
part of the remainder when num1 is divided by num2
rc = math_strlen(string)
string
any string variablerc
the length of string
rc = valnum(string)
string any string variablerc 0 - string
is not a number 1 -
string is an integer 2 -
string is not an integer
rc = wrdpos(string,int)
string
any string, usually contains more than one wordint
an integerrc
word #intstring starts at this character #
These math functions will only work with GrADS version 1.8 (or higher).
These script records were taken from a sample script called "script_math_demo.gs".
v = 3.1456
fmt = '%-6.1f'
rc = math_format(fmt,v)
say fmt' of 'v' = 'rc
pi = 3.1415926
d2r = pi/180
angd = 45
ang = angd * d2r
cos = math_cos(ang)
say 'cos of 'angd' = 'cos
num = '3.1455'
rc = valnum(num)
if (rc = 0) ; say num' is not a number' ; endif
if (rc = 1) ; say num' is an integer' ; endif
if (rc = 2) ; say num' is not an integer' ; endif
v = 3.0
while(v < 4.0)
rc1 = math_nint(v)
rc2 = math_int(v)
print 'nint of 'v' = 'rc1' int of 'v' = 'rc2
v = v + 0.1
endwhile
pow = math_pow(2,0.5);
print '2 raised to the power 0.5 = 'pow
num = math_exp(1)
print 'exp(1) = 'num
fmod = math_fmod(5,2)
print '5 modulo 2 (the remainder when 5 is divided by 2) = 'fmod
s = 'this is a test'
rc = math_strlen(s)
print 'length of the string "'s'" = 'rc
p = 2
rc = wrdpos(s,p)
print 'word 'p' of the string "'s'" starts at character 'rc