Additional mathematical routines.

This document describes the math unit. The math unit was initially written by Florian Klaempfl. It provides mathematical functions which aren't covered by the system unit.

This chapter starts out with a definition of all types and constants that are defined, after which an overview is presented of the available functions, grouped by category, and the last part contains a complete explanation of each function.

The following things must be taken into account when using this unit:

1. This unit is compiled in Object Pascal mode so all integers are 32 bit.
2. Some overloaded functions exist for data arrays of integers and floats. When using the address operator (@) to pass an array of data to such a function, make sure the address is typecasted to the right type, or turn on the 'typed address operator' feature. failing to do so, will cause the compiler not be able to decide which function you want to call.

Min/max determination

Functions to determine the minimum or maximum of numbers:

| Name | Description
| Maximum of 2 values
| Maximum of an array of integer values
| Maximum of an array of values
| Minimum of 2 values
| Minimum of an array of integer values
| Minimum of an array of values

Angle unit conversion

Routines to convert angles between different angle units.

| Name | Description
| convert cycles to radians
| convert degrees to grads
| convert degrees to radians
| convert grads to degrees
| convert grads to radians
| convert radians to cycles
| convert radians to degrees
| convert radians to grads

Trigoniometric functions

| Name | Description
| calculate reverse cosine
| calculate reverse sine
| calculate reverse tangent
| calculate cotangent
| calculate sine and cosine
| calculate tangent

Hyperbolic functions

| Name | Description
| caculate reverse hyperbolic cosine
| caculate reverse hyperbolic sine
| caculate reverse hyperbolic tangent
| calculate hyperbolic cosine
| calculate hyperbolic sine
| calculate hyperbolic tangent

Exponential and logarithmic functions

| Name | Description
| Raise float to integer power
| Calculate $2^p x$
| calculate log(x+1)
| calculate 10-base log
| calculate 2-base log
| calculate N-base log
| raise float to arbitrary power

Number converting

| Name | Description
| Round to infinity
| Round to minus infinity
| Return mantissa and exponent

Statistical functions

| Name | Description
| Mean of values
| Mean and standard deviation of values
| Moments, skew and kurtosis
| Population standarddeviation
| Population variance
| Gaussian distributed randum value
| Standard deviation
| Sum of values
| Sum of squared values
| Sum of values and squared values
| Total variance of values
| variance of values

Geometrical functions

| Name | Description
| Hypotenuse of triangle
| Euclidian norm

Float type used in all calls
All calculations are done with the Float type. This allows to recompile the unit with a different float type to obtain a desired precision. The pointer type is used in functions that accept an array of values of arbitrary length.
Pointer to type.
Type used in financial (interest) calculations.
End of period.
Start of period.
Exception raised when invalid arguments are passed to a function.
Return inverse cosine
Arccos
returns the inverse cosine of its argument x. The argument x should lie between -1 and 1 (borders included).
If the argument x is not in the allowed range, an EInvalidArgument exception is raised.
Return inverse hyperbolic cosine
Arcosh
returns the inverse hyperbolic cosine of its argument x. The argument x should be larger than 1.

The arccosh variant of this function is supplied for Delphi compatibility.
If the argument x is not in the allowed range, an EInvalidArgument exception is raised.
,
Return inverse sine
Arcsin
returns the inverse sine of its argument x. The argument x should lie between -1 and 1.
If the argument x is not in the allowed range, an EInvalidArgument exception is raised.
Return arctangent of (y/x)
arctan2
calculates arctan(y/x), and returns an angle in the correct quadrant. The returned angle will be in the range $-\pi$ to $\pi$ radians. The values of x and y must be between -2\^{}64 and 2\^{}64, moreover x should be different from zero.

On Intel systems this function is implemented with the native intel fpatan instruction.
If x is zero, an overflow error will occur.
Return inverse hyperbolic sine
arsinh
returns the inverse hyperbolic sine of its argument x.

The arscsinh variant of this function is supplied for Delphi compatibility.
None.
Return inverse hyperbolic tangent
artanh
returns the inverse hyperbolic tangent of its argument x, where x should lie in the interval [-1,1], borders included.

The arctanh variant of this function is supplied for Delphi compatibility.
In case x is not in the interval [-1,1], an EInvalidArgument exception is raised.
Return the lowest integer number greater than or equal to argument
Ceil
returns the lowest integer number greater than or equal to x. The absolute value of x should be less than maxint.
If the asolute value of x is larger than maxint, an overflow error will occur.
Return hyperbolic cosine
Cosh
returns the hyperbolic cosine of it's argument {x}.
None.
Return cotangent
Cotan
returns the cotangent of it's argument x. x should be different from zero.
If x is zero then a overflow error will occur.
Convert cycle angle to radians angle
Cycletorad
transforms it's argument cycle (an angle expressed in cycles) to radians. (1 cycle is $2 \pi$ radians).
None.
,
Convert degree angle to grads angle
Degtograd
transforms it's argument deg (an angle in degrees) to grads.

(90 degrees is 100 grad.)
None.
,
Convert degree angle to radians angle.
Degtorad
converts it's argument deg (an angle in degrees) to radians.

(pi radians is 180 degrees)
None.
,
Return the largest integer smaller than or equal to argument
Floor
returns the largest integer smaller than or equal to x. The absolute value of x should be less than maxint.
If x is larger than maxint, an overflow will occur.
Return mantissa and exponent.
Frexp
returns the mantissa and exponent of it's argument x in mantissa and exponent.
None
Convert grads angle to degrees angle
Gradtodeg
converts its argument grad (an angle in grads) to degrees.

(100 grad is 90 degrees)
None.
,
Convert grads angle to radians angle
Gradtorad
converts its argument grad (an angle in grads) to radians.

(200 grad is pi degrees).
None.
,
Return hypotenuse of triangle
Hypot
returns the hypotenuse of the triangle where the sides adjacent to the square angle have lengths x and y.

The function uses Pythagoras' rule for this.
None.
Return integer power.
Intpower
returns base to the power exponent, where exponent is an integer value.
If base is zero and the exponent is negative, then an overflow error will occur.
Return (2 to the power p) times x
Ldexp
returns (2 to the power p) times x.
None.
Return natural logarithm of 1+X
Lnxp1
returns the natural logarithm of 1+X. The result is more precise for small values of x. x should be larger than -1.
If $x\leq -1$ then an EInvalidArgument exception will be raised.
Return 10-Based logarithm.
Log10
returns the 10-base logarithm of X.
If x is less than or equal to 0 an 'invalid fpu operation' error will occur.
Return 2-based logarithm
Log2
returns the 2-base logarithm of X.
If x is less than or equal to 0 an 'invalid fpu operation' error will occur.
Return N-based logarithm.
Logn
returns the n-base logarithm of X.
If x is less than or equal to 0 an 'invalid fpu operation' error will occur.
Return largest of 2 values
Max
returns the maximum of Int1 and Int2.
None.
Return largest element in integer array

MaxIntValue returns the largest integer out of the Data array.

This function is provided for Delphi compatibility, use the function instead.

None.
Return largest value in array

Maxvalue returns the largest value in the data array with integer or float values. The return value has the same type as the elements of the array.

The third and fourth forms accept a pointer to an array of N integer or float values.

None.
Return mean value of array
Mean
returns the average value of data.

The second form accepts a pointer to an array of N values.
None.
Return mean and standard deviation of array
meanandstddev
calculates the mean and standard deviation of data and returns the result in mean and stddev, respectively. Stddev is zero if there is only one value.

The second form accepts a pointer to an array of N values.
None.
Return smallest of two values.
min
returns the smallest value of Int1 and Int2;
None.
Return smallest value in integer array

MinIntvalue returns the smallest value in the Data array.

This function is provided for Delphi compatibility, use minvalue instead.

None
Return smallest value in array

Minvalue returns the smallest value in the data array with integer or float values. The return value has the same type as the elements of the array.

The third and fourth forms accept a pointer to an array of N integer or float values.

None.
Return 4 first moments of distribution
momentskewkurtosis
calculates the 4 first moments of the distribution of valuesin data and returns them in m1,m2,m3 and m4, as well as the skew and kurtosis.
None.
Return Euclidian norm

Norm calculates the Euclidian norm of the array of data. This equals sqrt(sumofsquares(data)).

The second form accepts a pointer to an array of N values.

None.
Return population variance

Popnstddev returns the square root of the population variance of the values in the Data array. It returns zero if there is only one value.

The second form of this function accepts a pointer to an array of N values.

None.
Return population variance

Popnvariance returns the square root of the population variance of the values in the Data array. It returns zero if there is only one value.

The second form of this function accepts a pointer to an array of N values.

None.
,
Return real power.
power
raises base to the power power. This is equivalent to exp(power*ln(base)). Therefore base should be non-negative.
None.
Convert radians angle to cycle angle

Radtocycle converts its argument rad (an angle expressed in radians) to an angle in cycles.

(1 cycle equals 2 pi radians)

None.
,
Convert radians angle to degrees angle
Radtodeg
converts its argument rad (an angle expressed in radians) to an angle in degrees.

(180 degrees equals pi radians)
None.
,
Convert radians angle to grads angle

Radtodeg converts its argument rad (an angle expressed in radians) to an angle in grads.

(200 grads equals pi radians)

None.
,
Return gaussian distributed random number.
randg
returns a random number which - when produced in large quantities - has a Gaussian distribution with mean mean and standarddeviation stddev.
None.
Return sine and cosine of argument

Sincos calculates the sine and cosine of the angle theta, and returns the result in sinus and cosinus.

On Intel hardware, This calculation will be faster than making 2 calls to calculate the sine and cosine separately.

None.
.
Return hyperbolic sine
Sinh
returns the hyperbolic sine of its argument x.
Return standard deviation of data

Stddev returns the standard deviation of the values in Data. It returns zero if there is only one value.

The second form of the function accepts a pointer to an array of N values.

None.
Return sum of values

Sum returns the sum of the values in the data array.

The second form of the function accepts a pointer to an array of N values.

None.
Return sum of squares of values

Sumofsquares returns the sum of the squares of the values in the data array.

The second form of the function accepts a pointer to an array of N values.

None.
Return sum and sum of squares of values.

sumsandsquares calculates the sum of the values and the sum of the squares of the values in the data array and returns the results in sum and sumofsquares.

The second form of the function accepts a pointer to an array of N values.

None.
Return tangent
Tan
returns the tangent of x.
If x (normalized) is pi/2 or 3pi/2 then an overflow will occur.
Return hyperbolic tangent
Tanh
returns the hyperbolic tangent of x.
None.
Return total varians of values

TotalVariance returns the total variance of the values in the data array. It returns zero if there is only one value.

The second form of the function accepts a pointer to an array of N values.

None.
Return variance of values

Variance returns the variance of the values in the data array. It returns zero if there is only one value.

The second form of the function accepts a pointer to an array of N values.

None.
Used for exception definitions.
Minimum value (closest to zero) of extended type
Maximum value of extended type
Minimum value (closest to zero) of float type
Maximum value of float type
Pointer to integer type
Type to describe relational order between values
Values are the same
First value is less than second value
First values is greater than second value
Value is Not a Number
Value is infinity
Check whether value is in range.
InRange
returns True if AValue is in the range AMin..AMax. It returns False if Value lies outside the specified range.
Change value to it falls in specified range.
EnsureRange
returns Value if AValue is in the range AMin..AMax. It returns AMin if the value is less than AMin, or AMax if the value is larger than AMax.
Return DIV and MOD of arguments
DivMod
returns Dividend DIV Divisor in Result, and Dividend MOD Divisor in Remainder
Type indicating sign of a valuea
Value is negative
Value is zero
Value is positive
Return sign of argument
Sign
returns the sign of it's argument, which can be an Integer, 64 bit integer, or a double. The returned value is an integer which is -1, 0 or 1, and can be used to do further calculations with.
Check whether value is zero

IsZero checks whether the float value A is zero, up to a precision of Epsilon. It returns True if Abs(A) is less than Epsilon.

The default value for Epsilon is dependent on the type of the arguments, but is for the float type.

Check whether value is Not a Number
IsNan
returns True if the double d contains Not A Number (a value which cannot be represented correctly in double format).
Check whether value is infinite
IsInfinite
returns True if the double d contains the infinite value.
Check whether 2 float values are the same

SameValue returns True if the floating-point values A and B are the same, i.e. whether the absolute value of their their difference is smaller than Epsilon. If their difference is larger, then False is returned.

The default value for Epsilon is dependent on the type of the arguments, but is for the float type.

Return inverse hyperbolic cosine

arccosh returns the inverse hyperbolic cosine of it's argument x.

This function is an alias for , provided for Delphi compatibility.

Return inverse hyperbolic sine

arcsinh returns the inverse hyperbolic sine of it's argument x.

This function is an alias for , provided for Delphi compatibility.

Return inverse hyperbolic tangent

arcsinh returns the inverse hyperbolic tangent of it's argument x.

This function is an alias for , provided for Delphi compatibility.

Type describing the rounding mode for the Floating Point processor.
Round to nearest integer value
Round to biggest integer smaller than value.
Round to smallest integer larger than value.
Cut off fractional part.
Type describing the default precision for the Floating Point processor.
Single-type precision
?
Double-type precision
Extended-type precision
Type describing Floating Point processor exceptions.
Invalid operation error
Division by zero error.
Float overflow error
Float underflow error
Precision error
Type to set the Floating Point Unit exception mask.
Return the Floating Point Unit rounding mode.
Set the Floating Point Unit rounding mode.
Return the Floating Point Unit precision mode.
Set the Floating Point Unit precision mode.
Get the Floating Point Unit exception mask.
Set the Floating Point Unit exception mask.
Clear Floating Point Unit exceptions