Most programming languages have a built-in implementation of exponentiation.



Task

Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition).

If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both   intint   and   floatint   variants.



Related tasks

   Exponentiation order
   arbitrary-precision integers (included)
   Exponentiation with infix operators in (or operating on) the base


INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
INT FUNC PowerI(INT base,exp)
  INT res,i
 
  IF exp<0 THEN Break() FI
 
  res=1
  FOR i=1 TO exp
  DO
    res==*base
  OD
RETURN (res)
 
PROC PowerR(REAL POINTER base INT exp
            REAL POINTER res)
  INT i
  REAL tmp
 
  IF exp<0 THEN Break() FI
 
  IntToReal(1,res)
  FOR i=1 TO exp
  DO
    RealMult(res,base,tmp)
    RealAssign(tmp,res)
  OD
RETURN
 
PROC TestI(INT base,exp)
  INT res
 
  res=PowerI(base,exp)
  PrintF("%I^%I=%I%E",base,exp,res)
RETURN
 
PROC TestR(REAL POINTER base INT exp)
  REAL res
 
  PowerR(base,exp,res)
  PrintR(base) PrintF("^%I=",exp)
  PrintRE(res)
RETURN
 
PROC Main()
  REAL base
 
  Put(125) PutE() ;clear screen
 
  TestI(27,3)
  TestI(2,12)
  TestI(-3,9)
  TestI(1,1000)
  TestI(20000,0)
 
  ValR("3.141592654",base)
  TestR(base,10)
  ValR("-1.11",base)
  TestR(base,99)
  ValR("0.123456789",base)
  TestR(base,1)
  ValR("987654.321",base)
  TestR(base,0)
RETURN