User Tools

Site Tools


delphi:mathroutines

Math routines in Delphi

Introduction

Delphi's Built-in Maths Routines (Math.pas):

  • const { Ranges of the IEEE floating point types, including denormals }
    MinSingle = 1.5e-45;
    MaxSingle = 3.4e+38;
    MinDouble = 5.0e-324;
    MaxDouble = 1.7e+308;
    MinExtended = 3.4e-4932;
    MaxExtended = 1.1e+4932;
    MinComp = -9.223372036854775807e+18;
    MaxComp = 9.223372036854775807e+18;
  • All angle parameters and results of trig functions are in radians.

    Most of the following trig and log routines map directly to Intel 80387 FPU
    floating point machine instructions. Input domains, output ranges, and
    error handling are determined largely by the FPU hardware.
    Routines coded in assembler favor the Pentium FPU pipeline architecture.
    -----------------------------------------------------------------------}
  • Trigonometric functions
    function ArcCos(X: Extended): Extended; { IN: |X| <= 1 OUT: [0..PI] radians }
    function ArcSin(X: Extended): Extended; { IN: |X| <= 1 OUT: [-PI/2..PI/2] radians }

    { ArcTan2 calculates ArcTan(Y/X), and returns an angle in the correct quadrant.
    IN: |Y| < 2^64, |X| < 2^64, X <> 0 OUT: [-PI..PI] radians }
    function ArcTan2(Y, X: Extended): Extended;

    { SinCos is 2x faster than calling Sin and Cos separately for the same angle }
    procedure SinCos(Theta: Extended; var Sin, Cos: Extended) register;
    function Tan(X: Extended): Extended;
    function Cotan(X: Extended): Extended; { 1 / tan(X), X <> 0 }
    function Hypot(X, Y: Extended): Extended; { Sqrt(X**2 + Y**2) }
  • Angle unit conversion routines
    function DegToRad(Degrees: Extended): Extended; { Radians := Degrees * PI / 180}
    function RadToDeg(Radians: Extended): Extended; { Degrees := Radians * 180 / PI }
    function GradToRad(Grads: Extended): Extended; { Radians := Grads * PI / 200 }
    function RadToGrad(Radians: Extended): Extended; { Grads := Radians * 200 / PI }
    function CycleToRad(Cycles: Extended): Extended; { Radians := Cycles * 2PI }
    function RadToCycle(Radians: Extended): Extended;{ Cycles := Radians / 2PI }
  • Hyperbolic functions and inverses
    function Cosh(X: Extended): Extended;
    function Sinh(X: Extended): Extended;
    function Tanh(X: Extended): Extended;
    function ArcCosh(X: Extended): Extended; { IN: X >= 1 }
    function ArcSinh(X: Extended): Extended;
    function ArcTanh(X: Extended): Extended; { IN: |X| <= 1 }
  • Logarithmic functions
    function LnXP1(X: Extended): Extended; { Ln(X + 1), accurate for X near zero }
    function Log10(X: Extended): Extended; { Log base 10 of X}
    function Log2(X: Extended): Extended; { Log base 2 of X }
    function LogN(Base, X: Extended): Extended; { Log base N of X }
  • Exponential functions

    { IntPower: Raise base to an integral power. Fast. }
    function IntPower(Base: Extended; Exponent: Integer): Extended register;

    { Power: Raise base to any power.
    For fractional exponents, or |exponents| > MaxInt, base must be > 0. }
    function Power(Base, Exponent: Extended): Extended;

  • Miscellaneous Routines

    { Frexp: Separates the mantissa and exponent of X. }
    procedure Frexp(X: Extended; var Mantissa: Extended; var Exponent: Integer) register;

    { Ldexp: returns X*2**P }
    function Ldexp(X: Extended; P: Integer): Extended register;

    { Ceil: Smallest integer >= X, |X| < MaxInt }
    function Ceil(X: Extended):Integer;

    { Floor: Largest integer <= X, |X| < MaxInt }
    function Floor(X: Extended): Integer;

    { Poly: Evaluates a uniform polynomial of one variable at value X.
    The coefficients are ordered in increasing powers of X:
    Coefficients[0] + Coefficients[1]*X + ... + Coefficients[N]*(X**N) }
    function Poly(X: Extended; const Coefficients: array of Double): Extended;

    {-----------------------------------------------------------------------
  • Statistical functions.

    Common commercial spreadsheet macro names for these statistical and
    financial functions are given in the comments preceding each function.
    -----------------------------------------------------------------------}

    { Mean: Arithmetic average of values. (AVG): SUM / N }
    function Mean(const Data: array of Double): Extended;

    { Sum: Sum of values. (SUM) }
    function Sum(const Data: array of Double): Extended register;
    function SumInt(const Data: array of Integer): Integer register;
    function SumOfSquares(const Data: array of Double): Extended;
    procedure SumsAndSquares(const Data: array of Double;
    var Sum, SumOfSquares: Extended) register;

    { MinValue: Returns the smallest signed value in the data array (MIN) }
    function MinValue(const Data: array of Double): Double;
    function MinIntValue(const Data: array of Integer): Integer;

    function Min(A,B: Integer): Integer; overload;
    function Min(A,B: Int64): Int64; overload;
    function Min(A,B: Single): Single; overload;
    function Min(A,B: Double): Double; overload;
    function Min(A,B: Extended): Extended; overload;

    { MaxValue: Returns the largest signed value in the data array (MAX) }
    function MaxValue(const Data: array of Double): Double;
    function MaxIntValue(const Data: array of Integer): Integer;

    function Max(A,B: Integer): Integer; overload;
    function Max(A,B: Int64): Int64; overload;
    function Max(A,B: Single): Single; overload;
    function Max(A,B: Double): Double; overload;
    function Max(A,B: Extended): Extended; overload;

    { Standard Deviation (STD): Sqrt(Variance). aka Sample Standard Deviation }
    function StdDev(const Data: array of Double): Extended;

    { MeanAndStdDev calculates Mean and StdDev in one call. }
    procedure MeanAndStdDev(const Data: array of Double; var Mean, StdDev: Extended);

    { Population Standard Deviation (STDP): Sqrt(PopnVariance).
    Used in some business and financial calculations. }
    function PopnStdDev(const Data: array of Double): Extended;

    { Variance (VARS): TotalVariance / (N-1). aka Sample Variance }
    function Variance(const Data: array of Double): Extended;

    { Population Variance (VAR or VARP): TotalVariance/ N }
    function PopnVariance(const Data: array of Double): Extended;

    { Total Variance: SUM(i=1,N)[(X(i) - Mean)**2] }
    function TotalVariance(const Data: array of Double): Extended;

    { Norm: The Euclidean L2-norm. Sqrt(SumOfSquares) }
    function Norm(const Data: array of Double): Extended;

    { MomentSkewKurtosis: Calculates the core factors of statistical analysis:
    the first four moments plus the coefficients of skewness and kurtosis.
    M1 is the Mean. M2 is the Variance.
    Skew reflects symmetry of distribution: M3 / (M2**(3/2))
    Kurtosis reflects flatness of distribution: M4 / Sqr(M2) }
    procedure MomentSkewKurtosis(const Data: array of Double;
    var M1, M2, M3, M4, Skew, Kurtosis: Extended);

    { RandG produces random numbers with Gaussian distribution about the mean.
    Useful for simulating data with sampling errors. }
    function RandG(Mean, StdDev: Extended): Extended;

    {-----------------------------------------------------------------------
  • Financial functions. Standard set from Quattro Pro.

    Parameter conventions:

    From the point of view of A, amounts received by A are positive and
    amounts disbursed by A are negative (e.g. a borrower's loan repayments
    are regarded by the borrower as negative).

    Interest rates are per payment period. 11% annual percentage rate on a
    loan with 12 payments per year would be (11 / 100) / 12 = 0.00916667

    -----------------------------------------------------------------------}

    type
    TPaymentTime = (ptEndOfPeriod, ptStartOfPeriod);

    { Double Declining Balance (DDB) }
    function DoubleDecliningBalance(Cost, Salvage: Extended;
    Life, Period: Integer): Extended;

    { Future Value (FVAL) }
    function FutureValue(Rate: Extended; NPeriods: Integer; Payment, PresentValue:
    Extended; PaymentTime: TPaymentTime): Extended;

    { Interest Payment (IPAYMT) }
    function InterestPayment(Rate: Extended; Period, NPeriods: Integer; PresentValue,
    FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

    { Interest Rate (IRATE) }
    function InterestRate(NPeriods: Integer;
    Payment, PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

    { Internal Rate of Return. (IRR) Needs array of cash flows. }
    function InternalRateOfReturn(Guess: Extended;
    const CashFlows: array of Double): Extended;

    { Number of Periods (NPER) }
    function NumberOfPeriods(Rate, Payment, PresentValue, FutureValue: Extended;
    PaymentTime: TPaymentTime): Extended;

    { Net Present Value. (NPV) Needs array of cash flows. }
    function NetPresentValue(Rate: Extended; const CashFlows: array of Double;
    PaymentTime: TPaymentTime): Extended;

    { Payment (PAYMT) }
    function Payment(Rate: Extended; NPeriods: Integer;
    PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

    { Period Payment (PPAYMT) }
    function PeriodPayment(Rate: Extended; Period, NPeriods: Integer;
    PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

    { Present Value (PVAL) }
    function PresentValue(Rate: Extended; NPeriods: Integer;
    Payment, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

    { Straight Line depreciation (SLN) }
    function SLNDepreciation(Cost, Salvage: Extended; Life: Integer): Extended;

    { Sum-of-Years-Digits depreciation (SYD) }
    function SYDDepreciation(Cost, Salvage: Extended; Life, Period: Integer): Extended;

ESB Maths Routines (ESBMaths.pas):

  • various integer operations:
    • minimum, maximum & summation of arrays of various types
    •  
  • various floating point operations:
    • {: Returns the straight line Distance between (X1, Y1) and (X2, Y2) }
      function Distance (const X1, Y1, X2, Y2: Extended): Extended;

      {: Performs Floating Point Modulus. ExtMod := X - Floor ( X / Y ) * Y }
      function ExtMod (const X, Y: Extended): Extended;

      {: Performs Floating Point Remainder. ExtRem := X - Int ( X / Y ) * Y }
      function ExtRem (const X, Y: Extended): Extended;

      {: Returns X mod Y for Comp Data Types }
      function CompMOD (const X, Y: Comp): Comp;

      {: Converts Polar Co-ordinates into Cartesion Co-ordinates }
      procedure Polar2XY (const Rho, Theta: Extended; var X, Y: Extended);

      {: Converts Cartesian Co-ordinates to Polar Co-ordinates }
      procedure XY2Polar (const X, Y: Extended; var Rho, Theta: Extended);

      {: Converts Degrees/Minutes/Seconds into an Extended Real }
      function DMS2Extended (const Degs, Mins, Secs: Extended): Extended;

      {: Converts an Extended Real into Degrees/Minutes/Seconds }
      procedure Extended2DMS (const X: Extended; var Degs, Mins, Secs: Extended);
    • minimum, maximum & summation of arrays of various types
    • {: Returns the Sum of the Square of an Array of Extended Reals }
      function SumSqEArray (const B: array of Extended): Extended;

      {: Returns the Sum of the Square of the difference of
      an Array of Extended Reals from a given Value }
      function SumSqDiffEArray (const B: array of Extended; Diff: Extended): Extended;

      {: Returns the Sum of the Pairwise Product of two
      Arrays of Extended Reals }
      function SumXYEArray (const X, Y: array of Extended): Extended;

      {: Returns the Sum of an Array of Comp Reals }
      function SumCArray (const B: array of Comp): Comp;

      {: Returns A! i.e Factorial of A - only values up to 1547 are handled
      returns 0 if larger }
      function FactorialX (A: Cardinal): Extended;

      {: Returns nPr i.e Permutation of r objects from n.
      Only values of N up to 1547 are handled returns 0 if larger
      If R > N then 0 is returned }
      function PermutationX (N, R: Cardinal): Extended;

      {: Returns nCr i.e Combination of r objects from n.
      These are also known as the Binomial Coefficients
      Only values of N up to 1547 are handled returns 0 if larger
      If R > N then 0 is returned }

      function BinomialCoeff (N, R: Cardinal): Extended;

      {: Returns True if all elements of X > ESBTolerance }
      function IsPositiveEArray (const X: array of Extended): Boolean;

      {: Returns the Geometric Mean of the values }
      function GeometricMean (const X: array of Extended): Extended;

      {: Returns the Harmonic Mean of the values }
      function HarmonicMean (const X: array of Extended): Extended;

      {: Returns the Arithmetic Mean of the Values }
      function ESBMean (const X: array of Extended): Extended;

      {: Returns the Variance of the Values, assuming a Sample.
      Square root this value to get Standard Deviation }
      function SampleVariance (const X: array of Extended): Extended;

      {: Returns the Variance of the Values, assuming a Population.
      Square root this value to get Standard Deviation }
      function PopulationVariance (const X: array of Extended): Extended;

      {: Returns the Mean and Variance of the Values, assuming a Sample.
      Square root the Variance to get Standard Deviation }
      procedure SampleVarianceAndMean (const X: array of Extended;
      var Variance, Mean: Extended);

      {: Returns the Mean and Variance of the Values, assuming a Population.
      Square root the Variance to get Standard Deviation }
      procedure PopulationVarianceAndMean (const X: array of Extended;
      var Variance, Mean: Extended);

      {: Returns the Median (2nd Quartiles) of the Values. The array
      MUST be sorted before using this operation }
      function GetMedian (const SortedX: array of Extended): Extended;

      {: Returns the Mode (most frequent) of the Values. The array
      MUST be sorted before using this operation. Function is False
      if no Mode exists }
      function GetMode (const SortedX: array of Extended; var Mode: Extended): Boolean;

      {: Returns the 1st and 3rd Quartiles - Median is 2nd Quartiles - of the Values.
      The array MUST be sorted before using this operation }
      procedure GetQuartiles (const SortedX: array of Extended; var Q1, Q3: Extended);

 

 

delphi/mathroutines.txt · Last modified: 2021/07/24 11:54 by gary1