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


MODULE Expt EXPORTS Main;
 
IMPORT IO, Fmt;
 
PROCEDURE IntExpt(arg, exp: INTEGER): INTEGER =
  VAR result := 1;
  BEGIN
    FOR i := 1 TO exp DO
      result := result * arg;
    END;
    RETURN result;
  END IntExpt;
 
PROCEDURE RealExpt(arg: REAL; exp: INTEGER): REAL =
  VAR result := 1.0;
  BEGIN
    IF exp < 0 THEN
      FOR i := exp TO -1 DO
        result := result / arg;
      END;
    ELSE
      FOR i := 1 TO exp DO
        result := result * arg;
      END;
    END;
    RETURN result;
  END RealExpt;
 
BEGIN
  IO.Put("2 ^ 4 = " & Fmt.Int(IntExpt(2, 4)) & "\n");
  IO.Put("2.5 ^ 4 = " & Fmt.Real(RealExpt(2.5, 4)) & "\n");
END Expt.