>How can you set up a rexx function such that the result is rounded to
>the next lower whole number?
Here's one floor function... I'm sure there are better ones...
/* A fairly simple floor function in REXX */
/* Note: this procedure checks for text passed by mistake, and handles numbers
in scientific notation. It returns a value in scientific notation depending
on the current options of REXX. */
floor: procedure
parse arg num
if datatype(num,'N')=0
then return "" /* Return empty string if non-numeric */
num=format(num,,,0) /* Expand scientific format numbers */
period=pos('.',num) /* Find where the period is */
if period=0 /* No period, so... */
then return format(num) /* Return num in scientific format */
value=left(num,period-1)) /* Get the whole number portion */
if value<0 then value=value-1 /* Adjust for negative numbers */
return format(value) /* Return the floor in scientific format */
--
Albert Crosby | Microcomputer & Network Support | WANTED: any good
acr...@uafhp.uark.edu | University of Arkansas | "Intro To The Net
or AL.CROSBY on GENIE | College of Agriculture And | For Newbies" guides
1 501 575 4452 | Home Economics | (email for mine...)
TE>How can you set up a rexx function such that the result is rounded to
TE>the next lower whole number?
try using the TRUNC function
func:
arg x, y, z
/* your code here */
return trunc(your_result)
---
ş SLMR 2.1a ş
Use the % operator:
x%1
is x rounded down
"Round" means to change a number to the nearest integer. That is, 0.6
is rounded to 1, and 0.4 is rounded to 0. (Exactly .5 rounds up.)
"Truncate" means to change a number to the integer below. That is, 0.4
is truncated to 0, and 0.9 is also truncated to 0.
To "Round" as per the above definition: x = TRUNC(x+0.5)
To "Truncate" as per the above definition: x = TRUNC(x)
I prefer the TRUNC function over the % operator beause it is clearer
what your code is doing. TRUNC also has a nice option for specifying
how many digits to truncate to.
>period=pos('.',num) /* Find where the period is */
>if period=0 /* No period, so... */
> then return format(num) /* Return num in scientific format */
>value=left(num,period-1)) /* Get the whole number portion */
>if value<0 then value=value-1 /* Adjust for negative numbers */
You have to test whether the number is negative *before* you take only
the part before the period. Otherwise numbers between -1 and 0 result in
a value of 0 instead of -1.
Horst
- - - - - - - - - - - - - - - - - - - - - - -
Horst Kiehl - Internet h.p....@kfa-juelich.de
Doesn't this function just 'trunc', not round ???
keith
--
=============================================================
= Keith Hedger| "Look at 'em...fuckin' normals... =
= | I hate 'em." =
=============================================================