This chapter 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:
The following types are defined in the math unit:
Type Float = Extended; PFloat = ^FLoatAll 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.
Type TPaymentTime = (PTEndOfPeriod,PTStartOfPeriod);TPaymentTime is used in the financial calculations.
Type EInvalidArgument = Class(EMathError);The EInvalidArgument exception is used to report invalid arguments.
Program Example1; { Program to demonstrate the arccos function. } Uses math; Procedure WriteRadDeg(X : float); begin Writeln(X:8:5,' rad = ',radtodeg(x):8:5,' degrees.') end; begin WriteRadDeg (arccos(1)); WriteRadDeg (arccos(sqrt(3)/2)); WriteRadDeg (arccos(sqrt(2)/2)); WriteRadDeg (arccos(1/2)); WriteRadDeg (arccos(0)); WriteRadDeg (arccos(-1)); end.
The arccosh variant of this function is supplied for compatibility.
Program Example3; { Program to demonstrate the arcosh function. } Uses math; begin Writeln(arcosh(1)); Writeln(arcosh(2)); end.
Program Example1; { Program to demonstrate the arcsin function. } Uses math; Procedure WriteRadDeg(X : float); begin Writeln(X:8:5,' rad = ',radtodeg(x):8:5,' degrees.') end; begin WriteRadDeg (arcsin(1)); WriteRadDeg (arcsin(sqrt(3)/2)); WriteRadDeg (arcsin(sqrt(2)/2)); WriteRadDeg (arcsin(1/2)); WriteRadDeg (arcsin(0)); WriteRadDeg (arcsin(-1)); end.
On Intel systems this function is implemented with the native intel fpatan instruction.
Program Example6; { Program to demonstrate the arctan2 function. } Uses math; Procedure WriteRadDeg(X : float); begin Writeln(X:8:5,' rad = ',radtodeg(x):8:5,' degrees.') end; begin WriteRadDeg (arctan2(1,1)); end.
The arscsinh variant of this function is supplied for compatibility.
Program Example4; { Program to demonstrate the arsinh function. } Uses math; begin Writeln(arsinh(0)); Writeln(arsinh(1)); end.
The arctanh variant of this function is supplied for compatibility.
Program Example5; { Program to demonstrate the artanh function. } Uses math; begin Writeln(artanh(0)); Writeln(artanh(0.5)); end.
Program Example7; { Program to demonstrate the Ceil function. } Uses math; begin Writeln(Ceil(-3.7)); // should be -3 Writeln(Ceil(3.7)); // should be 4 Writeln(Ceil(-4.0)); // should be -4 end.
Program Example8; { Program to demonstrate the cosh function. } Uses math; begin Writeln(Cosh(0)); Writeln(Cosh(1)); end.
Program Example9; { Program to demonstrate the cotan function. } Uses math; begin writeln(cotan(pi/2)); Writeln(cotan(pi/3)); Writeln(cotan(pi/4)); end.
Program Example10; { Program to demonstrate the cycletorad function. } Uses math; begin writeln(cos(cycletorad(1/6))); // Should print 1/2 writeln(cos(cycletorad(1/8))); // should be sqrt(2)/2 end.
(90 degrees is 100 grad.)
Program Example11; { Program to demonstrate the degtograd function. } Uses math; begin writeln(degtograd(90)); writeln(degtograd(180)); writeln(degtograd(270)) end.
(pi radians is 180 degrees)
Program Example12; { Program to demonstrate the degtorad function. } Uses math; begin writeln(degtorad(45)); writeln(degtorad(90)); writeln(degtorad(180)); writeln(degtorad(270)); writeln(degtorad(360)); end.
Program Example13; { Program to demonstrate the floor function. } Uses math; begin Writeln(Ceil(-3.7)); // should be -4 Writeln(Ceil(3.7)); // should be 3 Writeln(Ceil(-4.0)); // should be -4 end.
Program Example14; { Program to demonstrate the frexp function. } Uses math; Procedure dofrexp(Const X : extended); var man : extended; exp: integer; begin man:=0; exp:=0; frexp(x,man,exp); write(x,' has '); Writeln('mantissa ',man,' and exponent ',exp); end; begin // dofrexp(1.00); dofrexp(1.02e-1); dofrexp(1.03e-2); dofrexp(1.02e1); dofrexp(1.03e2); end.
(100 grad is 90 degrees)
Program Example15; { Program to demonstrate the gradtodeg function. } Uses math; begin writeln(gradtodeg(100)); writeln(gradtodeg(200)); writeln(gradtodeg(300)); end.
(200 grad is pi degrees).
Program Example16; { Program to demonstrate the gradtorad function. } Uses math; begin writeln(gradtorad(100)); writeln(gradtorad(200)); writeln(gradtorad(300)); end.
The function uses Pythagoras' rule for this.
Program Example17; { Program to demonstrate the hypot function. } Uses math; begin Writeln(hypot(3,4)); // should be 5 end.
Program Example18; { Program to demonstrate the intpower function. } Uses math; Procedure DoIntpower (X : extended; Pow : Integer); begin writeln(X:8:4,'^',Pow:2,' = ',intpower(X,pow):8:4); end; begin dointpower(0.0,0); dointpower(1.0,0); dointpower(2.0,5); dointpower(4.0,3); dointpower(2.0,-1); dointpower(2.0,-2); dointpower(-2.0,4); dointpower(-4.0,3); end.
Program Example19; { Program to demonstrate the ldexp function. } Uses math; begin writeln(ldexp(2,4):8:4); writeln(ldexp(0.5,3):8:4); end.
Program Example20; { Program to demonstrate the lnxp1 function. } Uses math; begin writeln(lnxp1(0)); writeln(lnxp1(0.5)); writeln(lnxp1(1)); end.
Program Example21; { Program to demonstrate the log10 function. } Uses math; begin Writeln(Log10(10):8:4); Writeln(Log10(100):8:4); Writeln(Log10(1000):8:4); Writeln(Log10(1):8:4); Writeln(Log10(0.1):8:4); Writeln(Log10(0.01):8:4); Writeln(Log10(0.001):8:4); end.
Program Example22; { Program to demonstrate the log2 function. } Uses math; begin Writeln(Log2(2):8:4); Writeln(Log2(4):8:4); Writeln(Log2(8):8:4); Writeln(Log2(1):8:4); Writeln(Log2(0.5):8:4); Writeln(Log2(0.25):8:4); Writeln(Log2(0.125):8:4); end.
Program Example23; { Program to demonstrate the logn function. } Uses math; begin Writeln(Logn(3,4):8:4); Writeln(Logn(2,4):8:4); Writeln(Logn(6,9):8:4); Writeln(Logn(exp(1),exp(1)):8:4); Writeln(Logn(0.5,1):8:4); Writeln(Logn(0.25,3):8:4); Writeln(Logn(0.125,5):8:4); end.
Program Example24; { Program to demonstrate the max function. } Uses math; Var A,B : Cardinal; begin A:=1;b:=2; writeln(max(a,b)); end.
This function is provided for compatibility, use the maxvalue function instead.
Program Example25; { Program to demonstrate the MaxIntValue function. } { Make sore integer is 32 bit} {$mode objfpc} Uses math; Type TExArray = Array[1..100] of Integer; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 100 do ExArray[i]:=Random(I)-Random(100); Writeln(MaxIntValue(ExArray)); end.
The third and fourth forms accept a pointer to an array of N integer or float values.
Program Example26; { Program to demonstrate the MaxValue function. } { Make sore integer is 32 bit} {$mode objfpc} Uses math; Type TExFloatArray = Array[1..100] of Float; TExIntArray = Array[1..100] of Integer; Var I : Integer; ExFloatArray : TExFloatArray; ExIntArray : TExIntArray; AFLoatArray : PFLoat; AIntArray : PInteger; begin Randomize; AFloatArray:=@ExFloatArray[1]; AIntArray:=@ExIntArray[1]; for I:=1 to 100 do ExFloatArray[i]:=(Random-Random)*100; for I:=1 to 100 do ExIntArray[i]:=Random(I)-Random(100); Writeln('Max Float : ',MaxValue(ExFloatArray):8:4); Writeln('Max Float (b) : ',MaxValue(AFloatArray,100):8:4); Writeln('Max Integer : ',MaxValue(ExIntArray):8); Writeln('Max Integer (b) : ',MaxValue(AIntArray,100):8); end.
The second form accepts a pointer to an array of N values.
Program Example27; { Program to demonstrate the Mean function. } Uses math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; Writeln('Max : ',MaxValue(ExArray):8:4); Writeln('Min : ',MinValue(ExArray):8:4); Writeln('Mean : ',Mean(ExArray):8:4); Writeln('Mean (b) : ',Mean(@ExArray[1],100):8:4); end.
The second form accepts a pointer to an array of N values.
Program Example28; { Program to demonstrate the Meanandstddev function. } Uses math; Type TExArray = Array[1..100] of Extended; Var I : Integer; ExArray : TExArray; Mean,stddev : Extended; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; MeanAndStdDev(ExArray,Mean,StdDev); Writeln('Mean : ',Mean:8:4); Writeln('StdDev : ',StdDev:8:4); MeanAndStdDev(@ExArray[1],100,Mean,StdDev); Writeln('Mean (b) : ',Mean:8:4); Writeln('StdDev (b) : ',StdDev:8:4); end.
Program Example29; { Program to demonstrate the min function. } Uses math; Var A,B : Cardinal; begin A:=1;b:=2; writeln(min(a,b)); end.
This function is provided for compatibility, use minvalue instead.
Program Example30; { Program to demonstrate the MinIntValue function. } { Make sore integer is 32 bit} {$mode objfpc} Uses math; Type TExArray = Array[1..100] of Integer; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 100 do ExArray[i]:=Random(I)-Random(100); Writeln(MinIntValue(ExArray)); end.
The third and fourth forms accept a pointer to an array of N integer or float values.
Program Example26; { Program to demonstrate the MinValue function. } { Make sore integer is 32 bit} {$mode objfpc} Uses math; Type TExFloatArray = Array[1..100] of Float; TExIntArray = Array[1..100] of Integer; Var I : Integer; ExFloatArray : TExFloatArray; AFloatArray : PFloat; ExIntArray : TExIntArray; AintArray : PInteger; begin Randomize; AFloatArray:=@ExFloatArray[0]; AIntArray:=@ExIntArray[0]; for I:=1 to 100 do ExFloatArray[i]:=(Random-Random)*100; for I:=1 to 100 do ExIntArray[i]:=Random(I)-Random(100); Writeln('Min Float : ',MinValue(ExFloatArray):8:4); Writeln('Min Float (b) : ',MinValue(AFloatArray,100):8:4); Writeln('Min Integer : ',MinValue(ExIntArray):8); Writeln('Min Integer (b) : ',MinValue(AintArray,100):8); end.
Program Example32; { Program to demonstrate the momentskewkurtosis function. } Uses math; Var DistArray : Array[1..1000] of float; I : longint; m1,m2,m3,m4,skew,kurtosis : float; begin randomize; for I:=1 to 1000 do distarray[i]:=random; momentskewkurtosis(DistArray,m1,m2,m3,m4,skew,kurtosis); Writeln ('1st moment : ',m1:8:6); Writeln ('2nd moment : ',m2:8:6); Writeln ('3rd moment : ',m3:8:6); Writeln ('4th moment : ',m4:8:6); Writeln ('Skew : ',skew:8:6); Writeln ('kurtosis : ',kurtosis:8:6); end.
The second form accepts a pointer to an array of N values.
Program Example33; { Program to demonstrate the norm function. } Uses math; Type TVector = Array[1..10] of Float; Var AVector : Tvector; I : longint; begin for I:=1 to 10 do Avector[i]:=Random; Writeln(Norm(AVector)); end.
The second form of this function accepts a pointer to an array of N values.
Program Example35; { Program to demonstrate the PopnStdDev function. } Uses Math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; Writeln('Max : ',MaxValue(ExArray):8:4); Writeln('Min : ',MinValue(ExArray):8:4); Writeln('Pop. stddev. : ',PopnStdDev(ExArray):8:4); Writeln('Pop. stddev. (b) : ',PopnStdDev(@ExArray[1],100):8:4); end.
The second form of this function accepts a pointer to an array of N values.
Program Example36; { Program to demonstrate the PopnVariance function. } Uses math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; Writeln('Max : ',MaxValue(ExArray):8:4); Writeln('Min : ',MinValue(ExArray):8:4); Writeln('Pop. var. : ',PopnVariance(ExArray):8:4); Writeln('Pop. var. (b) : ',PopnVariance(@ExArray[1],100):8:4); end.
Program Example34; { Program to demonstrate the power function. } Uses Math; procedure dopower(x,y : float); begin writeln(x:8:6,'^',y:8:6,' = ',power(x,y):8:6) end; begin dopower(2,2); dopower(2,-2); dopower(2,0.0); end.
(1 cycle equals 2 pi radians)
Program Example37; { Program to demonstrate the radtocycle function. } Uses math; begin writeln(radtocycle(2*pi):8:6); writeln(radtocycle(pi):8:6); writeln(radtocycle(pi/2):8:6); end.
(180 degrees equals pi radians)
Program Example38; { Program to demonstrate the radtodeg function. } Uses math; begin writeln(radtodeg(2*pi):8:6); writeln(radtodeg(pi):8:6); writeln(radtodeg(pi/2):8:6); end.
(200 grads equals pi radians)
Program Example39; { Program to demonstrate the radtograd function. } Uses math; begin writeln(radtograd(2*pi):8:6); writeln(radtograd(pi):8:6); writeln(radtograd(pi/2):8:6); end.
Program Example40; { Program to demonstrate the randg function. } Uses Math; Type TExArray = Array[1..10000] of Float; Var I : Integer; ExArray : TExArray; Mean,stddev : Float; begin Randomize; for I:=1 to 10000 do ExArray[i]:=Randg(1,0.2); MeanAndStdDev(ExArray,Mean,StdDev); Writeln('Mean : ',Mean:8:4); Writeln('StdDev : ',StdDev:8:4); end.
On Intel hardware, This calculation will be faster than making 2 calls to clculatet he sine and cosine separately.
Program Example41; { Program to demonstrate the sincos function. } Uses math; Procedure dosincos(Angle : Float); Var Sine,Cosine : Float; begin sincos(angle,sine,cosine); Write('Angle : ',Angle:8:6); Write(' Sine :',sine:8:6); Write(' Cosine :',cosine:8:6); end; begin dosincos(pi); dosincos(pi/2); dosincos(pi/3); dosincos(pi/4); dosincos(pi/6); end.
Program Example42; { Program to demonstrate the sinh function. } Uses math; begin writeln(sinh(0)); writeln(sinh(1)); writeln(sinh(-1)); end.
The second form of the function accepts a pointer to an array of N values.
Program Example40; { Program to demonstrate the stddev function. } Uses Math; Type TExArray = Array[1..10000] of Float; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 10000 do ExArray[i]:=Randg(1,0.2); Writeln('StdDev : ',StdDev(ExArray):8:4); Writeln('StdDev (b) : ',StdDev(@ExArray[0],10000):8:4); end.
The second form of the function accepts a pointer to an array of N values.
Program Example44; { Program to demonstrate the Sum function. } Uses math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; Writeln('Max : ',MaxValue(ExArray):8:4); Writeln('Min : ',MinValue(ExArray):8:4); Writeln('Sum : ',Sum(ExArray):8:4); Writeln('Sum (b) : ',Sum(@ExArray[1],100):8:4); end.
The second form of the function accepts a pointer to an array of N values.
Program Example45; { Program to demonstrate the SumOfSquares function. } Uses math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; Writeln('Max : ',MaxValue(ExArray):8:4); Writeln('Min : ',MinValue(ExArray):8:4); Writeln('Sum squares : ',SumOfSquares(ExArray):8:4); Writeln('Sum squares (b) : ',SumOfSquares(@ExArray[1],100):8:4); end.
The second form of the function accepts a pointer to an array of N values.
Program Example45; { Program to demonstrate the SumOfSquares function. } Uses math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; s,ss : float; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; Writeln('Max : ',MaxValue(ExArray):8:4); Writeln('Min : ',MinValue(ExArray):8:4); SumsAndSquares(ExArray,S,SS); Writeln('Sum : ',S:8:4); Writeln('Sum squares : ',SS:8:4); SumsAndSquares(@ExArray[1],100,S,SS); Writeln('Sum (b) : ',S:8:4); Writeln('Sum squares (b) : ',SS:8:4); end.
Program Example47; { Program to demonstrate the Tan function. } Uses math; Procedure DoTan(Angle : Float); begin Write('Angle : ',RadToDeg(Angle):8:6); Writeln(' Tangent : ',Tan(Angle):8:6); end; begin DoTan(0); DoTan(Pi); DoTan(Pi/3); DoTAn(Pi/4); DoTan(Pi/6); end.
Program Example48; { Program to demonstrate the Tanh function. } Uses math; begin writeln(tanh(0)); writeln(tanh(1)); writeln(tanh(-1)); end.
The second form of the function accepts a pointer to an array of N values.
Program Example49; { Program to demonstrate the TotalVariance function. } Uses math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; TV : float; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; TV:=TotalVariance(ExArray); Writeln('Total variance : ',TV:8:4); TV:=TotalVariance(@ExArray[1],100); Writeln('Total Variance (b) : ',TV:8:4); end.
The second form of the function accepts a pointer to an array of N values.
Program Example50; { Program to demonstrate the Variance function. } Uses math; Type TExArray = Array[1..100] of Float; Var I : Integer; ExArray : TExArray; V : float; begin Randomize; for I:=1 to 100 do ExArray[i]:=(Random-Random)*100; V:=Variance(ExArray); Writeln('Variance : ',V:8:4); V:=Variance(@ExArray[1],100); Writeln('Variance (b) : ',V:8:4); end.