## Calculation of (num) 1/n (square root) using RPG

pts.
Tags:
C
Programming Languages
RPG
SQL
Do any one know how to handle below formula using RPG or else method which can be implement on AS400 RPG program : The formula is (number ) 1/n 1/n = mean 1 over n square root N= certain number and with decimal digit eg. 1.21 Please send me some suggestion about this . Many thanks Beside you can send mail to me using veronica_lui@mlchk.com thanks

## Answer Wiki

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

Are you using RPG/400 or RPG IV (ILE)?

If the latter you can use two asterisks for “raise to the power of”:

<pre>
C Eval Result = 125 ** (1/3)
</pre>

If you’re using RPG/400 then you could create an RPG IV program that accepts the value and root to be calculated and returns the result:

<pre>
D* Prototype
D ThisPgm pr ExtPgm(‘THISPGM’)
D iValue 15p 6
D iRoot 1p 0
D oResult 15p 6

D* Procedure Interface (*ENTRY PLIST)…
D ThisPgm pi
D iValue 15p 6
D iRoot 1p 0
D oResult 15p 6

C* Calculate the root. Note, if iRoot is zero then we close the program down
C* otherwise we calculate the root and leave it open…
C If iRoot = *Zeros
C Eval *InLR = *On
C Else
C Eval oResult = iValue ** (1 / iRoot)
C EndIf

C* Return to the calling program or close down…
C Return
</pre>

Another alternative if you don’t want to use RPG IV or ILE is to use embedded SQL as in the following example:

<pre>
C Z-ADD*ZEROS RESULT 156
C Z-ADD125 VALUE 156
C Z-ADD3 ROOT 10
**
C/EXEC SQL
C+ SET :RESULT = :VALUE ** (1 / :ROOT)
C/END-EXEC
**
C RESULT DSPLY
**
C MOVE *ON *INLR
</pre>

Hope it helps

Best regards

Jonathan

==================================================

Note that the system supports exponentiation. The _POWER MI builtin is available:
<pre>
H Debug
H dftactgrp( *NO )
H actgrp( *CALLER )

* Procedure Prototype (this.program)

D Exponent PR extpgm( ‘EXPONENT’ )

* Procedure Interface

D Exponent PI

D n s 30p 7
D e s 5p 3
D r s 30p 7

* Procedure Prototype (_POWER MI builtin)

D power pr 8f extproc( ‘_POWER’ )
D 8f value
D 8f value

C seton LR

C eval n = 100.47
C eval e = 2.0
C eval r = *zero

C eval r = power( n : e )
C dump

C eval r = power( 11.23 : 1.7 )
C dump

C return
</pre>
That example calls the builtin twice — once with declared variables and once with constants. The builtin operates on 64-bit floating-point values. I suspect that you can achieve just about any degree of precision you’d like.

Tom

## Discuss This Question:

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.