Sub-pgm is to calculate X to the power of Y and then return Z. (using RPGIV / RPGILE)

0 pts.
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
ASKED: April 22, 2005  1:39 AM
UPDATED: April 23, 2005  8:31 PM

Answer Wiki

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)

c) adding the lines:

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

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Dgrove
    Why do you need a sub program, as you can code EVAL Z=X**Y directly in your program? You should consider defining the result as a floating point field which allows +ve and -ve numbers 10 power-308 thru 10 power 308 with 16 significant digits if your potentail results are not within the range of a fixed packed decimal format
    0 pointsBadges:
    report
  • astradyne
    Hi Dgrove, I would guess because she isn't doing a straight forward "raise to the power of" calculation. The original question stated four rules: I have this in mind - > 4 cases: (1) If Y 1, then Z = X to the power of n which involves a couple of extra lines for conditions, etc. All the best Jonathan
    370 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following