Modula-2 math
last modified November 27, 2026 Modula-2
The arithmetic operators of Modula-2 stop at the four basic operations.
Everything else, from square roots to logarithms and sines, comes from
library modules. ISO Modula-2 has one module per real type:
RealMath for REAL, LongMath for
LONGREAL, and ComplexMath and
LongComplexMath for the complex types. Wirth's PIM had a
single, smaller module, MathLib0, which the last section
compares with them.
All examples are compiled with -fiso and print with
SRealIO from the console
output article. GNU Modula-2 15.2 has a few bugs in this area: a
round function that truncates, a printing error for values
just below 1, and a miscompiled complex power. Each one is
shown where it comes up, together with a way around it.
Square roots and powers
RealMath exports its functions under short names:
sqrt, power, exp, ln,
sin and so on. Importing the needed ones with
FROM lets a formula read as it does on paper.
MODULE roots;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT sqrt, power;
VAR
dx, dy: REAL;
BEGIN
WriteString("sqrt(2) = "); WriteFixed(sqrt(2.0), 6, 0); WriteLn;
WriteString("power(2, 10) = "); WriteFixed(power(2.0, 10.0), 1, 0); WriteLn;
WriteString("power(27, 1/3) = "); WriteFixed(power(27.0, 1.0 / 3.0), 6, 0); WriteLn;
WriteString("power(1.05, 10.5) = "); WriteFixed(power(1.05, 10.5), 6, 0); WriteLn;
dx := 4.0 - 1.0;
dy := 6.0 - 2.0;
WriteString("distance = "); WriteFixed(sqrt(dx * dx + dy * dy), 6, 0); WriteLn
END roots.
sqrt returns the positive square root, and
power(base, exponent) raises a positive base to any real
exponent, so it computes cube roots and fractional powers too. Modula-2
has no exponent operator. The last line is the distance between the
points (1, 2) and (4, 6), the square root of the sum of the squared
differences.
$ gm2 -fiso roots.mod -o roots $ ./roots sqrt(2) = 1.414214 power(2, 10) = 1024.0 power(27, 1/3) = 3.000000 power(1.05, 10.5) = 1.669120 distance = 5.000000
Exponentials and logarithms
exp and ln work with the base e, which
RealMath exports as the constant exp1. Logarithms
to other bases are ratios of natural logarithms.
MODULE logs;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT exp, ln, exp1;
PROCEDURE Log10(x: REAL): REAL;
BEGIN
RETURN ln(x) / ln(10.0)
END Log10;
PROCEDURE Log2(x: REAL): REAL;
BEGIN
RETURN ln(x) / ln(2.0)
END Log2;
VAR
rate: REAL;
BEGIN
WriteString("e = "); WriteFixed(exp1, 10, 0); WriteLn;
WriteString("exp(1) = "); WriteFixed(exp(1.0), 10, 0); WriteLn;
WriteString("ln(e) = "); WriteFixed(ln(exp1), 10, 0); WriteLn;
WriteString("log10(50) = "); WriteFixed(Log10(50.0), 6, 0); WriteLn;
WriteString("log2(50) = "); WriteFixed(Log2(50.0), 6, 0); WriteLn;
rate := 0.04;
WriteString("years to double at 4% = ");
WriteFixed(ln(2.0) / ln(1.0 + rate), 2, 0); WriteLn
END logs.
exp(1) and the constant exp1 agree to the ten
places shown, and ln undoes exp. The library has
no logarithm to base 10 or 2, so Log10 and Log2
divide by ln(10) and ln(2). The same rule gives
the time it takes an amount to double at 4% interest per year:
ln(2) / ln(1.04).
$ gm2 -fiso logs.mod -o logs $ ./logs e = 2.7182818285 exp(1) = 2.7182818285 ln(e) = 1.0000000000 log10(50) = 1.698970 log2(50) = 5.643856 years to double at 4% = 17.67
Trigonometry
The trigonometric functions take angles in radians. The constant
pi converts degrees: an angle in degrees times
pi / 180 is the same angle in radians.
MODULE angles;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT pi, sin, cos;
PROCEDURE Radians(degrees: REAL): REAL;
BEGIN
RETURN degrees * pi / 180.0
END Radians;
VAR
d: CARDINAL;
r: REAL;
BEGIN
WriteString("deg sin cos"); WriteLn;
FOR d := 0 TO 60 BY 15 DO
r := Radians(FLOAT(d));
WriteCard(d, 3);
WriteFixed(sin(r), 4, 8);
WriteFixed(cos(r), 4, 8);
WriteLn
END;
WriteString("circumference of r = 2.5: ");
WriteFixed(2.0 * pi * 2.5, 4, 0); WriteLn
END angles.
Radians does the conversion, and the loop prints the sine
and the cosine every 15 degrees. FLOAT turns the
CARDINAL loop variable into a REAL, because
d * pi would mix two types. The last line uses
pi for the circumference of a circle.
$ gm2 -fiso angles.mod -o angles $ ./angles deg sin cos 0 0.0000 1.0000 15 0.2588 0.9659 30 0.5000 0.8660 45 0.7071 0.7071 60 0.8660 0.5000 circumference of r = 2.5: 15.7080
Inverse functions
arcsin, arccos and arctan return
angles in radians. A common use of arctan is the direction of
a point (x, y) seen from the origin, but
arctan(y / x) alone cannot tell opposite quadrants apart.
MODULE atan;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT pi, arctan, arcsin, arccos;
PROCEDURE Degrees(radians: REAL): REAL;
BEGIN
RETURN radians * 180.0 / pi
END Degrees;
PROCEDURE Angle(x, y: REAL): REAL;
BEGIN
IF x > 0.0 THEN
RETURN arctan(y / x)
ELSIF x < 0.0 THEN
IF y >= 0.0 THEN RETURN arctan(y / x) + pi
ELSE RETURN arctan(y / x) - pi
END
ELSIF y > 0.0 THEN
RETURN pi / 2.0
ELSIF y < 0.0 THEN
RETURN -pi / 2.0
ELSE
RETURN 0.0
END
END Angle;
PROCEDURE Show(x, y: REAL);
BEGIN
WriteFixed(x, 1, 5); WriteFixed(y, 1, 5);
WriteFixed(Degrees(arctan(y / x)), 1, 8);
WriteFixed(Degrees(Angle(x, y)), 1, 8); WriteLn
END Show;
BEGIN
WriteString("arcsin(0.5) = "); WriteFixed(Degrees(arcsin(0.5)), 1, 0); WriteLn;
WriteString("arccos(0.5) = "); WriteFixed(Degrees(arccos(0.5)), 1, 0); WriteLn;
WriteString(" x y arctan Angle"); WriteLn;
Show(1.0, 1.0); Show(-1.0, 1.0); Show(-1.0, -1.0); Show(1.0, -1.0)
END atan.
arctan returns angles from -90 to 90 degrees, so the points
(1, 1) and (-1, -1) both give 45 degrees. Angle looks at the
signs of both coordinates and adds or subtracts pi when
x is negative, which gives a result from -180 to 180 degrees, the
function that C calls atan2. It also handles x = 0,
where y / x would divide by zero. ISO
RealMath has no such function; the
arg function of ComplexMath, shown later,
computes the same angle.
$ gm2 -fiso atan.mod -o atan
$ ./atan
arcsin(0.5) = 30.0
arccos(0.5) = 60.0
x y arctan Angle
1.0 1.0 45.0 45.0
-1.0 1.0 -45.0 135.0
-1.0 -1.0 45.0 -135.0
1.0 -1.0 -45.0 -45.0
Rounding
RealMath has a function round that should return
the nearest integer. In GNU Modula-2 15.2 it is implemented with
TRUNC and drops the fraction instead.
MODULE rounding;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteInt;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT round;
PROCEDURE Round(x: REAL): INTEGER;
BEGIN
IF x >= 0.0 THEN
RETURN INT(x + 0.5)
ELSE
RETURN -INT(-x + 0.5)
END
END Round;
VAR
value: REAL;
i: CARDINAL;
BEGIN
WriteString(" x round Round INT"); WriteLn;
value := -2.5;
FOR i := 1 TO 6 DO
WriteFixed(value, 1, 6);
WriteInt(round(value), 7);
WriteInt(Round(value), 7);
WriteInt(INT(value), 7);
WriteLn;
value := value + 1.2
END
END rounding.
The round column equals the INT column: 3.5
gives 3 and -2.5 gives -2. Round adds one half to the
magnitude and then truncates, which rounds halves away from zero.
INT is the ISO function that converts a real to an
INTEGER by truncation. TRUNC returns a
CARDINAL; it gives -2 for -2.7 in a default build, but a
build with runtime checks stops on the negative value. The
round of LongMath has the same bug.
$ gm2 -fiso rounding.mod -o rounding
$ ./rounding
x round Round INT
-2.5 -2 -3 -2
-1.3 -1 -1 -1
-0.1 0 0 0
1.1 1 1 1
2.3 2 2 2
3.5 3 4 3
Printing rounded values
SRealIO.WriteFixed rounds a value to the requested number of
places before it prints it. In 15.2 it gets this wrong when the rounding
carries into the units digit of a value below 1. Rounding the value first,
with a Round procedure as above, avoids the problem. The
procedure is useful in more than one program, so it goes into a module
together with a Fix function that rounds to a number of
places.
DEFINITION MODULE Reals; PROCEDURE Round(x: REAL): INTEGER; (* x rounded to the nearest integer, halves away from zero *) PROCEDURE Fix(x: REAL; places: CARDINAL): REAL; (* x rounded to the given number of decimal places *) END Reals.
IMPLEMENTATION MODULE Reals;
PROCEDURE Round(x: REAL): INTEGER;
BEGIN
IF x >= 0.0 THEN
RETURN INT(x + 0.5)
ELSE
RETURN -INT(-x + 0.5)
END
END Round;
PROCEDURE Fix(x: REAL; places: CARDINAL): REAL;
VAR
scale: REAL;
i: CARDINAL;
BEGIN
scale := 1.0;
FOR i := 1 TO places DO
scale := scale * 10.0
END;
RETURN FLOAT(Round(x * scale)) / scale
END Fix;
END Reals.
Fix multiplies by a power of ten, rounds to an integer and
divides again. The result is the nearest value with the given number of
decimals that a REAL can hold.
MODULE printing;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT pi, tan;
FROM Reals IMPORT Fix;
PROCEDURE Row(label: ARRAY OF CHAR; x: REAL);
BEGIN
WriteString(label);
WriteFixed(x, 4, 10);
WriteFixed(Fix(x, 4), 4, 10);
WriteLn
END Row;
BEGIN
WriteString(" direct rounded"); WriteLn;
Row("tan(30 deg)", tan(pi / 6.0));
Row("tan(45 deg)", tan(pi / 4.0));
Row("tan(60 deg)", tan(pi / 3.0));
Row("0.99996 ", 0.99996);
Row("0.00006 ", 0.00006)
END printing.
tan(pi / 4) is 0.9999999999999999 in binary floating point.
WriteFixed prints it as 01.000: a zero too many
in front and a decimal place too few. 0.00006 at four places becomes
00.000 instead of 0.0001. After Fix, the values
are 1.0 and 0.0001, and WriteFixed prints them correctly.
Values of 1 and above are printed correctly in any case.
$ gm2 -fiso -c Reals.mod
$ gm2 -fiso printing.mod Reals.o -o printing
$ ./printing
direct rounded
tan(30 deg) 0.5774 0.5774
tan(45 deg) 01.000 1.0000
tan(60 deg) 1.7321 1.7321
0.99996 01.000 1.0000
0.00006 00.000 0.0001
Domain errors
Some arguments have no real result: the square root of a negative number,
the logarithm of zero or of a negative number, the arcsine of a value
above 1. ISO says that RealMath raises an exception for them,
but GNU Modula-2 returns the IEEE special values instead.
MODULE domain;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT sqrt, ln, arcsin;
PROCEDURE Show(label: ARRAY OF CHAR; x: REAL);
BEGIN
WriteString(label); WriteFixed(x, 4, 0);
IF x # x THEN
WriteString(" (not a number)")
END;
WriteLn
END Show;
BEGIN
Show("sqrt(-1) = ", sqrt(-1.0));
Show("ln(-1) = ", ln(-1.0));
Show("arcsin(2) = ", arcsin(2.0));
Show("ln(0) = ", ln(0.0));
Show("1 / 0 = ", 1.0 / 0.0);
Show("sqrt(2) = ", sqrt(2.0))
END domain.
A result that is not a number, NaN, prints as NAN or
-NAN, and an infinite one as INF or
-INF. NaN is the only value that is not equal to itself, so
x # x detects it. Dividing a real by zero is not an error
either and gives an infinity. A NaN spreads through every calculation that uses
it, so a program should check its arguments before calling the function.
$ gm2 -fiso domain.mod -o domain $ ./domain sqrt(-1) = -NAN (not a number) ln(-1) = -NAN (not a number) arcsin(2) = NAN (not a number) ln(0) = -INF 1 / 0 = INF sqrt(2) = 1.4142
Domain errors with checks
The runtime checks from the error
handling article also look at real values. With
-fsoft-check-all, assigning a NaN to a variable stops the
program.
MODULE checked;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT sqrt;
VAR
area, side: REAL;
BEGIN
area := 2.0;
side := sqrt(area);
WriteString("side = "); WriteFixed(side, 4, 0); WriteLn;
area := -2.0;
side := sqrt(area);
WriteString("side = "); WriteFixed(side, 4, 0); WriteLn
END checked.
The default build prints -NAN for the second side and goes
on. The checked build stops at the assignment of the second
sqrt, line 15, with an odd message about REAL
not overlapping with REAL, and status 134. The output line
before it lacks its line end because the ISO channel holds back the last
line mark, as described in the console output article.
$ gm2 -fiso checked.mod -o checked $ ./checked side = 1.4142 side = -NAN $ gm2 -fiso -fsoft-check-all checked.mod -o checked $ ./checked side = 1.4142checked.mod:15:3: In program module checked checked.mod:15:3:assignment will cause a range error, as the runtime instance value of ‘REAL’ does not overlap with the type ‘REAL’ $ echo $? 134
Long reals
LONGREAL on x86-64 is the 80-bit extended format with about
19 significant digits, against 16 for REAL.
LongMath has the same functions as RealMath for
it, and SLongIO prints it. Both pairs export the same names,
so the program imports them qualified.
MODULE precision;
FROM STextIO IMPORT WriteString, WriteLn;
IMPORT RealMath, LongMath, SRealIO, SLongIO;
VAR
x: REAL;
lx: LONGREAL;
i: CARDINAL;
BEGIN
WriteString("REAL "); SRealIO.WriteFixed(RealMath.sqrt(2.0), 18, 0); WriteLn;
WriteString("LONGREAL "); SLongIO.WriteFixed(LongMath.sqrt(2.0), 18, 0); WriteLn;
WriteString("REAL "); SRealIO.WriteFixed(RealMath.pi, 18, 0); WriteLn;
WriteString("LONGREAL "); SLongIO.WriteFixed(LongMath.pi, 18, 0); WriteLn;
x := 0.0;
lx := 0.0;
FOR i := 1 TO 1000000 DO
x := x + 0.1;
lx := lx + 0.1
END;
WriteString("REAL "); SRealIO.WriteFixed(x, 10, 0); WriteLn;
WriteString("LONGREAL "); SLongIO.WriteFixed(lx, 10, 0); WriteLn
END precision.
The first four lines print 18 places. The REAL values are
correct to about 16 digits and then drift, while the
LONGREAL ones are correct to the last place. The loop adds
0.1, which has no exact binary form, a million times. The
REAL sum is off in the sixth decimal, the
LONGREAL sum only in the tenth.
$ gm2 -fiso precision.mod -o precision $ ./precision REAL 1.414213562373095145 LONGREAL 1.414213562373095049 REAL 3.141592653589793116 LONGREAL 3.141592653589793239 REAL 100000.0000013329 LONGREAL 100000.0000000009
Complex numbers
ISO Modula-2 has a built-in type COMPLEX. CMPLX
builds a value from its real and imaginary parts, RE and
IM take them apart, and the four arithmetic operators and
= work on it directly. No module is needed for that.
MODULE complex;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
PROCEDURE Show(label: ARRAY OF CHAR; z: COMPLEX);
BEGIN
WriteString(label);
WriteFixed(RE(z), 2, 0);
IF IM(z) < 0.0 THEN WriteString(" - ") ELSE WriteString(" + ") END;
WriteFixed(ABS(IM(z)), 2, 0);
WriteString("i");
WriteLn
END Show;
VAR
a, b: COMPLEX;
BEGIN
a := CMPLX(3.0, 4.0);
b := CMPLX(1.0, -2.0);
Show("a = ", a);
Show("b = ", b);
Show("a + b = ", a + b);
Show("a - b = ", a - b);
Show("a * b = ", a * b);
Show("a / b = ", a / b);
IF a * b = CMPLX(11.0, -2.0) THEN
WriteString("a * b equals 11 - 2i"); WriteLn
END
END complex.
Show prints a complex value in the usual form, with the sign
of the imaginary part between the two parts. The product of 3 + 4i and
1 - 2i is 11 - 2i, and dividing by 1 - 2i undoes multiplying by it. Two
complex values compare with = when both their parts are
equal, which with floating-point results is as fragile as with reals.
LONGCOMPLEX is the same type built on
LONGREAL.
$ gm2 -fiso complex.mod -o complex $ ./complex a = 3.00 + 4.00i b = 1.00 - 2.00i a + b = 4.00 + 2.00i a - b = 2.00 + 6.00i a * b = 11.00 - 2.00i a / b = -1.00 + 2.00i a * b equals 11 - 2i
ComplexMath
ComplexMath provides the functions for COMPLEX
values, under the same names as RealMath, and the constants
i, one and zero. The printing
procedure now rounds both parts with Fix first, because
results such as exp(pi i) have imaginary parts of about
10-16.
MODULE cmath;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM RealMath IMPORT pi;
FROM ComplexMath IMPORT i, abs, arg, conj, sqrt, exp, polarToComplex, scalarMult;
FROM Reals IMPORT Fix;
PROCEDURE Show(label: ARRAY OF CHAR; z: COMPLEX);
VAR
re, im: REAL;
BEGIN
re := Fix(RE(z), 4);
im := Fix(IM(z), 4);
WriteString(label);
WriteFixed(re, 4, 0);
IF im < 0.0 THEN WriteString(" - ") ELSE WriteString(" + ") END;
WriteFixed(ABS(im), 4, 0);
WriteString("i");
WriteLn
END Show;
VAR
w: COMPLEX;
BEGIN
w := CMPLX(3.0, 4.0);
WriteString("abs(w) = "); WriteFixed(abs(w), 4, 0); WriteLn;
WriteString("arg(w) = "); WriteFixed(arg(w), 4, 0); WriteLn;
Show("conj(w) = ", conj(w));
Show("sqrt(w) = ", sqrt(w));
Show("sqrt(-4) = ", sqrt(CMPLX(-4.0, 0.0)));
Show("i * i = ", i * i);
Show("exp(pi i) = ", exp(scalarMult(pi, i)));
Show("polarToComplex(2, pi/2) = ", polarToComplex(2.0, pi / 2.0))
END cmath.
abs is the length of the number seen as a vector and
arg its angle, the same angle as Angle(3, 4) in
the inverse functions section. conj flips the sign of the
imaginary part, and sqrt works for negative real numbers too.
scalarMult multiplies by a real number, so
exp(scalarMult(pi, i)) is Euler's eiπ = -1.
polarToComplex builds a number from its length and angle.
$ gm2 -fiso cmath.mod Reals.o -o cmath $ ./cmath abs(w) = 5.0000 arg(w) = 0.9273 conj(w) = 3.0000 - 4.0000i sqrt(w) = 2.0000 + 1.0000i sqrt(-4) = 0.0000 + 2.0000i i * i = -1.0000 + 0.0000i exp(pi i) = -1.0000 + 0.0000i polarToComplex(2, pi/2) = 0.0000 + 2.0000i
A miscompiled power
ComplexMath.power raises a complex number to a real power.
GNU Modula-2 15.2 compiles it as a built-in function, and the default
build gets the result wrong when the base is a variable.
MODULE cpower;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM ComplexMath IMPORT power;
PROCEDURE Show(label: ARRAY OF CHAR; z: COMPLEX);
BEGIN
WriteString(label);
WriteFixed(RE(z), 4, 9);
WriteFixed(IM(z), 4, 9);
WriteLn
END Show;
VAR
w: COMPLEX;
BEGIN
w := CMPLX(3.0, 4.0);
Show("w * w ", w * w);
Show("power(CMPLX(3, 4), 2) ", power(CMPLX(3.0, 4.0), 2.0));
Show("power(w, 2) ", power(w, 2.0))
END cpower.
(3 + 4i)2 is -7 + 24i. Multiplication gives it, and so does
power with a constant base, but power(w, 2)
returns a different number. With -O2 all three lines agree,
which points to the code generator rather than to the library. Until this
is fixed, use multiplication for whole powers and
exp(scalarMult(e, ln(z))) for others.
LongComplexMath.power is wrong in both builds: it returns
NaN without optimisation and 1.38 for the real part with -O2.
$ gm2 -fiso cpower.mod -o cpower $ ./cpower w * w -7.0000 24.0000 power(CMPLX(3, 4), 2) -7.0000 24.0000 power(w, 2) -0.2600 0.5545 $ gm2 -fiso -O2 cpower.mod -o cpower $ ./cpower w * w -7.0000 24.0000 power(CMPLX(3, 4), 2) -7.0000 24.0000 power(w, 2) -7.0000 24.0000
Roots of a quadratic equation
A quadratic equation ax2 + bx + c = 0 has two real roots, one double root or two complex roots, depending on the sign of the discriminant b2 - 4ac. With complex arithmetic one formula covers all three cases.
MODULE quadratic;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SRealIO IMPORT WriteFixed;
FROM ComplexMath IMPORT sqrt;
FROM Reals IMPORT Fix;
PROCEDURE Show(z: COMPLEX);
VAR
re, im: REAL;
BEGIN
re := Fix(RE(z), 4);
im := Fix(IM(z), 4);
WriteFixed(re, 4, 0);
IF im # 0.0 THEN
IF im < 0.0 THEN WriteString(" - ") ELSE WriteString(" + ") END;
WriteFixed(ABS(im), 4, 0);
WriteString("i")
END
END Show;
PROCEDURE Solve(a, b, c: REAL);
VAR
d: COMPLEX;
BEGIN
d := sqrt(CMPLX(b * b - 4.0 * a * c, 0.0));
WriteFixed(a, 1, 5); WriteFixed(b, 1, 5); WriteFixed(c, 1, 5);
WriteString(" ");
Show((CMPLX(-b, 0.0) + d) / CMPLX(2.0 * a, 0.0));
WriteString(", ");
Show((CMPLX(-b, 0.0) - d) / CMPLX(2.0 * a, 0.0));
WriteLn
END Solve;
BEGIN
WriteString(" a b c roots"); WriteLn;
Solve(1.0, -3.0, 2.0);
Solve(1.0, 2.0, 1.0);
Solve(1.0, 2.0, 5.0);
Solve(2.0, 0.0, 1.0)
END quadratic.
Solve takes the complex square root of the discriminant, so
a negative discriminant needs no special case. The coefficients are
turned into complex values with CMPLX, because
COMPLEX and REAL do not mix in an expression.
Show leaves out an imaginary part of zero, so real roots
print as plain numbers.
$ gm2 -fiso quadratic.mod Reals.o -o quadratic
$ ./quadratic
a b c roots
1.0 -3.0 2.0 2.0000, 1.0000
1.0 2.0 1.0 -1.0000, -1.0000
1.0 2.0 5.0 -1.0000 + 2.0000i, -1.0000 - 2.0000i
2.0 0.0 1.0 0.0000 + 0.7071i, 0.0000 - 0.7071i
PIM MathLib0
Wirth's MathLib0 has sqrt, exp,
ln, sin, cos, arctan
and entier; GNU Modula-2 adds tan and the two
constants. The module is also available with -fiso, which
helps when older code is ported.
MODULE pim;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteInt;
FROM SRealIO IMPORT WriteFixed;
IMPORT MathLib0, RealMath;
VAR
x: REAL;
BEGIN
WriteString("MathLib0.sqrt(2) = "); WriteFixed(MathLib0.sqrt(2.0), 6, 0); WriteLn;
WriteString("RealMath.sqrt(2) = "); WriteFixed(RealMath.sqrt(2.0), 6, 0); WriteLn;
WriteString("MathLib0.arctan(1) = "); WriteFixed(MathLib0.arctan(1.0), 6, 0); WriteLn;
WriteString("RealMath.arctan(1) = "); WriteFixed(RealMath.arctan(1.0), 6, 0); WriteLn;
x := -2.7;
WriteString("MathLib0.entier(-2.7) = "); WriteInt(MathLib0.entier(x), 0); WriteLn;
WriteString("INT(-2.7) = "); WriteInt(INT(x), 0); WriteLn;
WriteString("RealMath.round(-2.7) = "); WriteInt(RealMath.round(x), 0); WriteLn
END pim.
The common functions give the same results. MathLib0 has no
power, arcsin or arccos; PIM
programs compute them from exp, ln and
arctan. entier rounds down to the next lower
integer, so -2.7 gives -3, while INT and the broken
round both truncate toward zero and give -2. New code should
use RealMath, which is the standard module.
$ gm2 -fiso pim.mod -o pim $ ./pim MathLib0.sqrt(2) = 1.414214 RealMath.sqrt(2) = 1.414214 MathLib0.arctan(1) = 0.785398 RealMath.arctan(1) = 0.785398 MathLib0.entier(-2.7) = -3 INT(-2.7) = -2 RealMath.round(-2.7) = -2
Source
The ISO libraries in GNU Modula-2
In this article we have computed square roots, powers, logarithms and
trigonometric functions with RealMath, written our own
rounding and two-argument arctangent, worked around a printing bug, seen
what happens with arguments outside a function's domain, used
LONGREAL for more digits, and calculated with complex
numbers. The data types article covers
the sizes of the real types, the
type conversions article
FLOAT and TRUNC, and the
console output article the
SRealIO procedures.
Author
List all Modula-2 tutorials.