While RPG contains some basic mathematical functions, it is missing some important, advanced functions. For example, we needed a logarithmic function to calculate bond yield. Since RPG does not supply a logarithmic function, I wrote my own program called LOG001RG, shown in 8. To understand how to use this program, let me explain a little bit about how logarithms work.
While RPG contains some basic mathematical functions, it is missing some important, advanced functions. For example, we needed a logarithmic function to calculate bond yield. Since RPG does not supply a logarithmic function, I wrote my own program called LOG001RG, shown in Figure 8. To understand how to use this program, let me explain a little bit about how logarithms work.
The definition of a logarithm is as follows: The logarithm of a number is the exponential power to which a number must be raised to produce a given number.
The calculation of a logarithm is expressed as y = logb x, which is stated "y equals log to the base b of x".
Two bases are commonly used when calculating logarithms. Log to the base 10 is called the common logarithm. The other base is a special number often referred to as "e", with a value of 2.718281828.... This type of logarithm is called the natural logarithm. Most scientific calculators have two buttons for calculating logarithms. The button for calculating common logarithms is normally labeled log while the one for calculating natural logarithms is usually labled ln.
The LOG001RG program calculates both common and natural logarithms. Use the first parameter to specify which type of logarithm you want to calculate. Pass the parameter a 'C' to calculate the common logarithm or an 'N' to calculate the natural logarithm. Use the second parameter to pass your input number. The program passes back your answer in the third parameter.
RPG Logarithm Calculation
Figure 8 Logarithmic Calculation
*=============================================================== * To compile: * * CRTRPGPGM PGM(XXX/LOG001RG) SRCFILE(XXX/QRPGSRC) * *=============================================================== *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 * Square root of natural log base e: 2.718281828 I 1.648721271 C ESQRT * Common log base 10 I 2.302585093 C COMLOG * C *ENTRY PLIST C PARM LOGTYP 1 C PARM PARMI 159 C PARM PARMO 159 * C Z-ADD0 PARMO C Z-ADDPARMI X 159 * Input parm if < 0 or = 1, then the output is 0 C X IFGT 0 C X ANDNE1 * If 1 > X > 0, then result is negative, LOG(X)=-LOG(1/X) C X IFLT 1 C Z-ADD-1 SIGN 10 C 1 DIV X X H C ELSE C Z-ADD1 SIGN C ENDIF * Divide X by ESQRT until 1 < X <= ESQRT, each time add .5 C X DOWGTESQRT C DIV ESQRT X H C ADD 0.5 PARMO C ENDDO * Use the Taylor Series to calculate the output: * LOG(1 + X) = X - X ** 2/2 + X ** 3/3 - X ** 4/4 ... * and add the result to the final result C SUB 1 X C Z-ADDX TERM 159 C Z-ADD1 N 50 C TERM1 DOUEQ0 C TERM DIV N TERM1 159H C ADD TERM1 PARMO C ADD 1 N C MULT X TERM H C MULT -1 TERM C ENDDO * Multiply the sign C MULT SIGN PARMO * Calculate common logarithm if necessary C LOGTYP IFEQ 'C' C DIV COMLOG PARMO H C ENDIF C ENDIF * C MOVE *ON *INLR