## Sub-pgm is to calculate X to the power of Y and then return Z. (using RPGIV / RPGILE)  Tags:
COBOL
Programming Languages
RPG
SQL
Hello Because I am new to RPGIV (existing using RPGIII) . Recently I need to write a sub-program using RPGIV (or RPGILE ? ). This sub-program will receive two parmerter (X , Y) and output one parmeter (Z) (which are in numeric format - BTW what is the max precision for numeric format in RPGIV / RPGILE? (i.e. max no of decimal points supported) ) The value of Y can be a negative number or number in value between 0 and 1 (ie. Y is a fraction number of 1/n) or number of value > 1) The function of this sub-pgm is to calculate X to the power of Y and then return Z. I have this in mind - > 4 cases: (1) If Y < 0 (ie. -Y) , then Z = 1/ X to the power of +Y) (2) If Y = 0 , then Z = 0 (3) If Y between 0 and 1 (a fraction of 1/n) , then Z= X to the power of n (with decimal digit) (4) If Y > 1, then Z = X to the power of n Examples as below: if X=9 and n=2 then X**n = 81 (i.e 9**2) if x=9 and n=0.5 (i.e. 1/2) then X**n =3 (i.e. square root) Can anyone give some idea to me : How to implement this login in RPGIV (if possible, can it be show in source code ?) Beside , Can this sub-program written in RPGIV / RPGILE be called by a traditional RPGIII program ? Many many thanks You can send directly to my mail box : veronica_lui@mlchk.com
Related Questions

Thanks. We'll let you know when a new response is added.

Hi

The maximum precision used to be 15 decimal places, I don’t know if it has changed at all. Using packed numerics, the maximum size number is 30 digits including decimal places, so a 30.15 packed size would give you 15 digits before and 15 digits after the decimal point.

If Y=0 why do you want Z=0? X**0 mathematically equals 1.

The following *SRVPGM should do what you want (if I’ve read the requirements properly):

PROTOTYPE
=========

* Test to see if prototype already defined..

D/If NOT DEFINED(PROTOTYPE)
D/Define PROTOTYPE
D/Else
D/Eof
D/EndIf

* Prototype for GetPower function…

D GetPower Pr 30p15
D Value 30p15 Const
D Power 30p15 Const

SERVICE PROGRAM
===============

H NOMAIN DATFMT(*YMD) OPTION(*NODEBUGIO:*SRCSTMT)

******************************************************************
** P R O T O T Y P E D E F I N I T I O N S **
******************************************************************
/Copy Examples/Qcpysrc,GetPower

******************************************************************
** P R O C E D U R E S **
******************************************************************

* GetPower
* 16:07, 31 July 2007 (CDT)[[User:12.148.252.66|12.148.252.66]]
P GetPower b Export

* Procedure interface
D GetPower pi 30p15
D Value 30p15 Const
D Power 30p15 Const

* Calculate the power and return the result…
C Select

C When Power < 0
C Return (1/Value)**%abs(Power)
C Return 1/(Value**%abs(Power))

C When Power = 0
C Return 0

C When (Power > 0) and (Power < 1)
C Return Value**Power

C Other
C Return -1
C EndSl

P GetPower e

To call the function you will need to bind the service program into your RPG ILE program and then in the source you would have a line similar to:

Eval Z=GetPower(X:Y)

One thing to note, I wasn’t sure about the interpretation of case (1) “If Y<0 (i.e. -Y) then Z=1/X to the power of +Y)”.

Is this 1 divided by the calculation of X to the power of +Y, or the result of 1 being divided by X and then raised to the power of +Y. I have put two Return statements in the section that deals with Y<0, you’ll have to remove the one you don’t want.

The GetPower procedure doesn’t validate whether or not -Y has any decimals? Should it? And if Y is greater than 1 then the value -1 is returned to act as an error indicator.

The service program can’t be called from RPG III type programs as it is an ILE object. If you want that functionality then modify the source by:

a) removing NOMAIN from the H spec

b) adding an entry list for fields X, Y and Z (all 30.15)

Eval Z=GetPower(X:Y)
Return

d) Create the program using the CRTBNDRPG command.

The resulting program can then be called from an RPG program.

Hope that helps.

All the best

Jonathan

## Discuss This Question: 2 Replies 