16.12.2012 Views

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

# restore the old scale<br />

scale=old_scale<br />

return (x)<br />

3. Here is a function you can define to return the fractional part of a number:<br />

define fractional_part(x) {return (x - integer_part(x))}<br />

4. The following function lets you set the scale of number to a given number of<br />

decimal places:<br />

define set_scale(x, s)<br />

{ auto os<br />

os = scale<br />

scale = s<br />

x /= 1<br />

scale = os<br />

return (x) }<br />

You can now use set_scale() in a function that rounds a number to two decimal<br />

places:<br />

define round2(num) {<br />

auto temp;<br />

if(scale(num) < 2) return (set_scale(num, 2))<br />

temp = (num - set_scale(num, 2)) * 1000<br />

if(temp > 5) num += 0.01<br />

return (set_scale(num,2))<br />

}<br />

This is a very useful function if you want to work with monetary values. For<br />

example, you can now rewrite sales_tax() to use round2():<br />

define sales_tax(purchase,tax) {<br />

auto old_scale<br />

scale = 2<br />

tax = round2(purchase*(tax/100))<br />

scale = old_scale<br />

return (tax)<br />

}<br />

5. Here is a function that recursively calculates the factorial of its argument:<br />

define fact (x) {<br />

if(x < 1) return 1<br />

return (x*fact(x-1))<br />

}<br />

You can also write the factorial function iteratively:<br />

define fact (x) {<br />

auto result<br />

result = 1<br />

while(x>1) result *= x--<br />

return (result)<br />

}<br />

With either version, fact(6) returns 720.<br />

6. Here is another recursive function, that calculates the nth element of the<br />

Fibonacci sequence:<br />

define fib(n) {<br />

if(n < 3) {<br />

return (1)<br />

} else {<br />

return (fib(n-1)+fib(n-2))<br />

}<br />

}<br />

bc<br />

Chapter 2. Shell command descriptions 63

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!