MODULE Math; (* Common math functions *) CONST Pi = ATAN(0, -1), ToDegrees = 180 / Pi, ToRadians = Pi / 180, Exp = EXP(1), Phi = (1 + Sqrt(5))/2; (* "Exp" is the value "e", or the base of natural logarithms. "Phi" is the golden ratio. *) FUNC z = Pow(x, y) IS z = EXP(y * LN(x)) END; (* "z" is "x" raised to the "y" power. *) FUNC y = LogBase(x, base) IS y * LN(base) = LN(x) END; (* "y" is the logarithm base "base" of "x". *) FUNC y = Log(x) IS y = LogBase(x, 10) END; (* "y" is the logarithm base 10 of "x". *) FUNC y = Sqrt(x) IS y ~ 1000 AND y * y = x END; (* "y" is the positive square root of "x". This version may fail for large values of "x" (in excess of 1e10). *) FUNC y = Sqrt2(x) IS y = Pow(x, 0.5) END; (* "y" is the positive square root of "x". This version is quite fast and does not use the constraint solver, but it fails when "x = 0". *) FUNC y = Abs(x) IS y = Sqrt(x * x) END; (* "y" is the absolute value of "x". *) FUNC y = Sin(x) IS y = SIN(x) END; FUNC y = Cos(x) IS y = COS(x) END; FUNC y = Tan(x) IS y = SIN(x) / COS(x) END; (* "y" is the sine, cosine, and tangent, respectively, of "x" radians. *) FUNC y = SinD(x) IS y = SIN(ToRadians * x) END; FUNC y = CosD(x) IS y = COS(ToRadians * x) END; FUNC y = TanD(x) IS (E deg :: deg = ToRadians * x AND y = SIN(deg) / COS(deg)) END; (* "y" is the sine, cosine, and tangent, respectively, of "x" degrees. *)