MATH --
a unit of elementary mathematical routines for Turbo
Pascal versions 4, 5, 5.5, and 6.
Copyright 1990, 1992, by J. W. Rider
MATH.PAS
provides a library of mathematical constants, type
definitions,
functions, and procedures for immediate use within
contemporary
Turbo Pascal programs (tested with versions 5.5 and
6).
Routines are provided for the following categories:
Numerical rounding and fractioning:
ceil, floor, modf, remf,
round2, sgn
Binary operators:
atan2, fmod, fdiv, frem,
hypot, ipow, iroot,
max, min, pow, powi, root,
round2, sign
Conditional operators:
iif
Polynomial evaluation:
dpoly, poly, xpoly
Exponential and power-raising
functions:
exp, exp2, exp10, frexp, ipow,
iroot, ldexp, pow,
powi, pow10, root, sqrt
Logarithmic functions:
ln, log, log2, log10
Circular trigonometric functions and
inverses:
acos, acosd, asin, asind,
atan, atan2, atan2d,
atand, cosd, sind, tan, tand
Degree/radian conversions for circular
trig:
deg2dms, deg2rad, dms2deg, dms2rad,
rad2deg,
rad2dms
Hyperbolic trigonometric functions and
inverses:
acosh, asinh, atanh, cosh,
sinh, tanh
Complex numbers
cabs
Some
caveats:
MATH.PAS redefines the following
SYSTEM unit functions:
exp, ln, pi, sqrt
In order to improve performance, none
of the MATH
routines
perform any range-checking for invalid values.
Generally,
this only means that the result returned will be
equally
invalid. However, there are some
special values that
WILL
result in run-time errors for some of the routines. Calling
the
MATH routines with illegal or improper inputs is strictly at
the
programmer's risk.
While many of the MATH.PAS routines
have parallels in
Turbo
C, there are some exceptions where the behavior could be
astonishingly
different. This has to do with how "MOD" should be
interpreted. In Turbo C, (and, in Turbo Pascal, as well)
"MOD"
is
considered to be what is left-over after integer division. In
MATH.PAS,
"MOD" is considered to be a mathematical "modulus"
function. For what it is that is left over after
division, the
example
of the Ada programming language is followed with the use
of the
term "REM" (for "remainder"). If you never try to use the
related
functions with negative arguments, you'll never have to
worry
about whether the difference is important.
MATH.PAS
constant definitions
These are provided as a programmer
convenience. The
routines
in MATH.PAS use many, but not all, of them.
However,
there
is no particular need for the application program to refer
to them
at all.
CBRT2 the cube root of 2
CBRT3 the cube root of 3
D2R the number of radians in one degree of arc
E base of natural logarithms
EULERC Euler's constant
LN2 natural logarithm of 2
LN3 natural logarithm of 3
LN10 natural logarithm of 10
LNPI
natural logarithm of PI
LOG2E 1/ln(2)
LOG10E 1/ln(10)
M2R the number of radians in one minute of arc
PI plain ol' round pi -- the ratio of the
circumference of a circle to
its diameter. (Note:
this constant replaces the
"pi" function of the
SYSTEM unit.)
PI_2 pi/2
PI_4 pi/4
R1_E 1/e
R1_PI 1/pi
R1_SQRTPI 1/sqrt(pi)
R2_PI 2/pi
R2_SQRTPI 2/sqrt(pi)
R2D number of degrees of arc in a radian
S2R the number of radians in one second of arc
SQRPI this is a square, vice round, pi
SQRT_2 sqrt(1/2)
SQRT2 sqrt(2)
SQRT2PI sqrt(2*pi)
SQRT3 sqrt(3)
SQRT5 sqrt(5)
SQRTPI sqrt(pi) or gamma(1/2)
TWOPI 2*pi
MATH.PAS
function and procedure definitions
name (parameter list) type of result
---- ---------------- --------------
acos (x: extended) extended
Return the principal value of the
inverse circular cosine
of "x" for abs(x)<=1. The returned value is in radians
and ranges between 0 and PI. Related functions:
"acosd","asin",
"atan", "system.cos".
acosd (x: extended) extended
Returns the principal value of the
inverse circular
cosine of "x" for
abs(x)<=1. The returned value is in
decimal degrees and ranges between 0
and 180. Related
functions: "acos",
"cosd".
acosh (x: extended) extended
Returns the principal value of the
inverse hyperbolic
cosine of "x" for
x>=1. The returned value is greater
than or equal to 0. Related functions:
"asinh", "atanh",
"cosh".
asin (x: extended) extended
Returns the principal value of the
inverse circular sine
of "x" for
abs(x)<=1. The returned value is in
radians
and ranges between -PI/2 and
+PI/2. Related functions:
"acos", "asind",
"atan", "system.sin".
asind (x: extended) extended
Returns the principal value of the
inverse circular sine
of "x" for
abs(x)<=1. The returned value is in
decimal
degrees and ranges between -90 and
+90. Related
functions: "asin",
"sind".
asinh (x: extended) extended
Returns the inverse hyperbolic sine of
"x". Related
functions: "acosh",
"atanh", "sinh".
atan (x: extended) extended
Returns the principal value of the
inverse circular
tangent of "x". The returned value is in radians and
ranges between -PI/2 and +PI/2. Note: this is the same
as "arctan" in the SYSTEM
unit. This function is
provided for nomenclature consistency
only. Related
functions: "acos",
"asin", "atan2", "tan".
atan2 (x, y: extended) extended
Returns the angle, in radians, between
the x-axis and the
line segment connecting the origin and
the coordinate
(x,y). This is the angular component generated in
converting from cartesian coordinates
(x,y) to polar.
The range of ATAN2 is bounded between
-PI and +PI
radians. Related functions: "atan", "hypot",
"tan".
atan2d (x, y: extended) extended
Returns the angle, in decimal degrees,
between the x-axis
and the line segment connecting the
origin and the
coordinate (x,y). This is the angular component
generated in converting from cartesian
coordinates (x,y)
to polar. The range of ATAN2D is
bounded between -180 and
+180 degrees. Related functions: "atan2",
"tand".
atand (x: extended) extended
Returns the principal value of the
inverse circular
tangent of "x". The returned value is in decimal
degrees, and ranges between -90 and
+90. Related
functions: "system.arctan",
"tand".
atanh (x: extended) extended
Returns the inverse hyperbolic tangent
of "x" for
abs(x)<1. Related functions: "acosh",
"asinh", "tanh".
atof (s: string) extended
Extracts a floating point number from
a string.
cabs (z: complex) extended
Returns the absolute value of the complex number "z".
This
routine is provided for other language
compatibility only.
Another "cabs" is defined in
the CMATH unit that expects
an "xcomplex" (extended
complex) number as an argument.
ceil (x: extended) extended
Returns the smallest integer value
greater than or equal
to "x". (Rounds up.) Related functions: "floor" (rounds
down), "system.int" (rounds
toward zero), "system.round"
(rounds to nearest integer),
"round2" (rounds to
specified number of decimal places).
cosd (x: extended) extended
Returns the circular cosine of
"x" degrees. COSD ranges
between -1 and +1. Related functions: "acosd",
"system.cos".
cosh (x: extended) extended
Returns the hyperbolic cosine of
"x". The returned value
is greater than or equal to 0. Related functions:
"acosh", "sinh",
"tanh".
deg2dms
(deg: extended; var d,m,s :float) ------
Converts "deg" degrees into
degree-minute-second
notation. Related functions: "deg2rad", "dms2deg".
deg2rad
(x: extended) extended
Converts "x" degrees into
radians. Related functions:
"deg2dms",
"rad2deg".
degrees
(x: extended) extended
Converts "x" radians into
degrees. Provided for compatibility
with some non-programming language
applications.
dms2deg
(d,m,s: extended) extended
Converts degrees-minute-seconds into
decimals degrees.
Related functions:
"deg2dms", "dms2rad".
dms2rad
(d,m,s: extended) extended
Converts degrees-minute-seconds into
decimals radians.
Related functions:
"rad2dms", "dms2deg".
dpoly (x: extended; degree: integer; var
coeffs) extended
Same as "poly", but expects
"coeffs" to be an array of
double.
exp (x: extended) extended
Returns the value of "e"
(the base of the natural
logarithms) raised to the
"x" power. If x is
sufficiently negative, merely returns
0. Otherwise, it
calls "system.exp". Note: this function is provided as a
replacement for the "exp" of
the SYSTEM unit which
overflows (!) when "x" is
very negative. Related
functions: "system.exp",
"exp2", "exp10", "math.ln",
"pow", "root".
exp2 (x: extended) extended
Returns the value of "2"
raised to the "x" power.
Related functions:
"math.exp", "exp10", "log2".
exp10 (x: extended) extended
Returns the value of "10"
raised to the "x" power.
Related functions: "math.exp",
"exp2", "log10", "pow10".
fabs (x: extended) extended
Returns the absolute value of
"x". Provided for
compatibility
only.
fdiv (x, y: extended) extended
Returns the number of times that
"y" divides evenly into
"x" (rounds toward 0) -- a
floating point analogue of the
integer "DIV" operator. Related functions: "fmod",
"frem", "remf".
floor (x: extended) extended
Returns the largest integer value less
than or equal to
"x". (Rounds down.) Related functions: "ceil" (rounds
up), "system.int" (rounds
toward zero), "system.round"
(rounds toward nearest integer),
"round2" (rounds to
specified number of decimal places).
fmod (x, y: extended) extended
Returns the modulus that results from
evenly dividing "y"
into "x" (rounded
down). For constant "y",
"fmod" is a
periodic function of
"x". (This is NOT the same as
Turbo
C's "fmod" nor Turbo
Pascal's MOD operator for integers!)
Related functions: "frem",
"modf".
frem (x, y: extended) extended
Returns the remainder that results
after evenly dividing
"y" into "x"
(rounded toward 0). For constant
"y",
"frem" is an odd function of
"x". A floating point
analogue of the integer MOD operator.)
Related functions:
"fdiv", "fmod",
"remf".
frexp (x: extended; var epart:longint) extended
Divides "x" into two parts:
a mantissa m and an exponent
e such that x=m*pow(2,e). The exponent is returned in
"epart". The mantissa is returned as the result of
the
function. (For normalized "x" values, 0.5<=abs(m)<1.)
Related function: "ldexp".
hypot (x, y: extended) extended
Returns the hypoteneuse of the right
triangle with sides
"x" and "y". This is the range component generated in
converting cartesian coordinates (x,y)
to polar
coordinates. Related functions: "atan2".
iif (p: boolean; t, f: extended) extended
Returns "t" if "p"
is true; otherwise returns "f".
Note:
this routine will force the
unnecessary evaluation of
either "t" or "f"
expressions without regard to which
value is returned. It
works more efficiently if "t" and
"f" are variables vice
expressions. This function is
used within several places in the
source code where that
situation arises.
ipow (x, y: extended) extended
Returns the imaginary component of the
complex value that
results when real "x" is
raised the real "y" power. (Function
"pow" returns the real
component.) There may be an
infinite number of satisfactory
values, and this function
only returns one. Related functions: "iroot",
"pow".
iroot (x, y: extended) extended
Returns the imaginary component of the
complex value that
results when taking the real
"y" root of the real "x"
value. (Function "root"
returns the real component.)
There may be an infinite number of
satisfactory values,
and this function only returns one.
Related functions:
"ipow", "root".
ldexp (x: extended; epart: longint) extended
Returns x*pow(2,epart). Related function: "frexp".
ln (x: extended) extended
Returns the natural logarithm of
"x". If x<0, it returns
the real component of the complex
logarithm value. (The
imaginary component is
"pi".) Note: a simpler
version
of "ln" exists in the SYSTEM
unit. Related functions:
"math.exp", "log",
"log2", "log10".
log (x: extended) extended
Returns the natural logarithm of
"x". Related functions:
"math.ln", "log2",
"log10".
log2 (x: extended) extended
Returns the base two logarithm of
"x". (Base two
logarithms are used extensively in
information theory.)
Related functions: "exp2", "math.ln",
"log", "log10".
log10 (x: extended) extended
Returns the common or base ten
logarithm of "x". Related
functions: "exp10",
"math.ln", "log", "log2", "pow10".
max (x, y: extended) extended
Returns either "x" or
"y", whichever is greater.
Related
functions: "min".
min (x, y: extended) extended
Returns either "x" or
"y", whichever is less. Related
functions: "max".
modf (x: extended; var ipart: float) extended
Divides the value of "x"
into an integer part and a
fractional part. The integer part (rounded down --
making it different from Turbo C's
"modf" and Turbo
Pascal's integer MOD operator) is
returned in "ipart".
The fractional part is return as the
value of the
function. Related functions:
"fmod", "remf".
poly (x: extended; degree: integer; var coeffs
) extended
Returns the evaluation of the
polynomial
2 degree
coeffs[0]+coeffs[1]*X+coeffs[2]*X
+...+coeffs[degree]*X
IMPORTANT! The untyped var
"coeffs" is presumed to be an
array of "float", but the
compiler does not enforce this
restriction. Passing any other structure to "poly" will
generate inexplicable results.
pow (x, y: extended) extended
Returns "x" raised the
"y" power. Uses an exponential
-
logarithm algorithm. Related functions: "math.exp",
"ipow", "powi",
"root", "system.sqr".
pow10 (x: extended) extended
Returns ten raised to the
"x" power. Same thing as
"exp10". This function is provided for certain
programmers who are accustomed to
exponentiating "10"
with a function of this name.
powi (x: extended; n: integer) extended
Returns "x" raised to the
"n" power. Uses a recursive
squaring - multiplication
algorithm. Related functions:
"pow",
"system.sqr".
rad2deg
(x: extended) extended
Converts "x" radians into
degrees. Related functions:
"deg2rad".
rad2dms
(r:extended; var d,m,s: float) ------
Converts "r" radians into
degrees-minute-seconds. Related
functions: "dms2rad",
"rad2deg".
radians
(x: extended) extended
Converts "x" degrees into
radians. Provided for compatibility
with non-programming language
applications.
remf (x: extended; var ipart: float) extended
Divides the value of "x"
into an integer part and a
fractional part. The integer part (rounded toward 0) is
returned in "ipart". The fractional part is return as
the value of the function. Related functions: "fdiv",
"frem", "modf".
For an example of use, see the source
code for the procedure
"deg2dms".
root (x, y: extended) extended
Returns "x" raised to the
"y" power. If x<0, then
the
real component of the complex power is
returned. Related
functions: "pow",
"system.sqrt".
round2 (x: extended; n: shortint) extended
Returns "x", rounded to
"n" decimal places to the right
of the decimal point. If "n" is negative, rounding
occurs to the left of the decimal
point. NOTE! Because
of the approximate nature of storing
fractional
components in floating point
representations, you should
be never assume that the result of
"round2" is *exact*.
However, it should be very, very
close.
sgn (x: extended) extended
Returns the "signum" of
"x". If x>0, then
"+1" is
returned. If x<0, "-1" is returned. If x=0, then "0"
is returned. Related functions: "system.abs".
sign (x, y: extended) extended
Returns .... Provided for
compatibility with Fortran language
programs.
sind (x: extended) extended
Returns the circular sine of
"x" degrees. SIND ranges
from -1 to +1. Related functions: "asind",
"system.sin".
sinh (x: extended) extended
Returns the hyperbolic sine of "x". Related functions:
"asinh", "cosh",
"tanh".
sqrt (x: extended) extended
Returns the square root of
"x". If x>=0, then
SYSTEM.SQRT is invoked. For x<0, this function returns
the negative of system.sqrt(-x). This makes "math.sqrt"
an odd function. Also, note that "math.sqrt(-1) <>
root(-1,2)". Instead, "math.sqrt(-1) =
-iroot(-1,2)".
Related functions: "root",
"system.sqr", "system.sqrt".
tan (x: extended) extended
Returns the circular tangent of
"x". The value of
"x" is
assumed to be expressed in
radians. Related functions:
"system.sin",
"system.cos", "system.arctan".
tand (x: extended) extended
Returns the circular tangent of
"x" degrees. Related
functions: "atand",
"tan".
tanh (x: extended) extended
Returns the hyperbolic tangent of
"x". TANH ranges from
-1 to 1. Related functions:
"atanh", "cosh", "sinh".
xpoly (x: extended; degree: integer; var
coeffs) extended
Same as "poly" but expects
"coeffs" to be an array of
"extended".