RosettaCodeData/Task/Exponentiation-operator/Modula-3/exponentiation-operator.mod3
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

32 lines
644 B
Text

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.