TechTip: RPG IV Exponent Support Rocks!

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
RPG programmers have been limited in their use of mathematical operations, especially before RPG IV. Originally, only add, subtract, multiply, and divide/mvr were available, but these were sufficient for most business applications. Computing integer powers was simply done by multiplying a field as many times as necessary. The need for square roots prompted IBM to provide the SQRT operation code in RPG II. In RPG IV, the %sqrt built-in function also finds a square root. But what can you do about exponents with mixed numbers, fractions, and negative numbers?

If I remember my math classes correctly, this sort of thing requires logarithms. With the ** operator, there is no problem! Use the ** operator in extended factor 2 or free-form calculations to specify exponentiation. The compiler generates code that uses floating-point arithmetic to perform the math. The result is then converted to decimal and moved into the specified answer or work field. If the definition of the answer field is too small to contain the result, the system compiler issues a floating point conversion exception, rather than passing a partial or invalid value.

To demonstrate the power of this capability, I've provided examples below to show how to code the computation of a future value. Financial math books include the formulas that I used in coding these examples.

First, let's compute the future value of a sum of money placed in an investment (like a savings account or certificate of deposit). After the initial investment, no additional money is added. Knowing the interest rate and the number of time periods, we can determine the future value.

If we let i equal the annual interest rate divided by 12, and n equal the total number of months between now and the future date, the formula is as follows:

Future Value (Single amount) = Present Value * ( (1 + i) ^ n )

The caret symbol (^) means "raised to the power of."

A single RPG IV calculation can perform this math:

Eval(hr)  FVsingle = PresentVal * ((1+i)**n)


Next, let's compute the future value of a sum of money that has periodic payments only.
Using the i and n as before, the formula is as follows:

Future Value (Annuity) = Periodic payment * ( ( (1+i) ^ n - 1) / i)

An RPG calculation can perform this math as well:

Eval(hr)  FVAnn = PerPayment * (((1+i)**n -1)/i)


In the Eval's above, the operation extenders h (half adjust) and r (precision) were used to handle accuracy and precision issues.

To get a combination of both a single amount invested over a period of time and the future value of an annuity, simply run both lines of the above code and then add them together.

A real life application: If you place $5,000 in an investment account earning 5% annual interest and also put $100 per month into this account, how much money would be in the account after 10 years?

The $5000 grows to $8235.05 in 10 years, and the $100 per month grows to $15,528.23, so the answer is the sum of these two: $23,763.28.

Exponentiation in RPG IV is a powerful tool that we can use to make our application programming jobs easier.

Jim Martin is corporate technical instructor at Jack Henry & Associates in Monett, Missouri. He is a veteran of RPG programming, beginning in 1967 with a position at IBM as a systems engineer and later becoming a staff programmer at the Rochester systems programming lab. For eight years, he was at Lakeview Technology as an AS/400 and RPG instructor and was a speaker at various local midrange user group meetings and conferences. He can be reached by email at This email address is being protected from spambots. You need JavaScript enabled to view it..



JIM MARTIN

Jim Martin holds a BS degree in mathematics and an MS in computer science. For 26 years, he was employed by IBM, where he wrote RPG applications for customers and worked in the programming laboratory as a programmer on portions of CPF and the OS/400 operating system. After leaving IBM, Jim took post-graduate work in computer science and performed RPG training. He is an IBM-certified RPG IV developer and author of multiple bestselling editions of Free-Format RPG IV, which, since the book's initial publication in 2005, have taught thousands of RPG IV programmers how to be successful with the free-format coding style.


MC Press books written by Jim Martin available now on the MC Press Bookstore.

Free-Format RPG IV: Third Edition Free-Format RPG IV: Third Edition
Improve productivity, readability, and program maintenance with the free-format style of programming in RPG IV.
List Price $59.95

Now On Sale

Free-Format RPG IV: Second Edition Free-Format RPG IV: Second Edition
>Make the transition from coding in fixed-format RPG to free format.
List Price $59.95

Now On Sale

Functions in Free-Format RPG IV Functions in Free-Format RPG IV
Here’s the ultimate guide to writing RPG IV programs with functions in the free-format style.
List Price $59.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: