home *** CD-ROM | disk | FTP | other *** search
-
- {*******************************************************}
- { }
- { Borland Delphi Runtime Library }
- { Math Unit }
- { }
- { Copyright (C) 1996,99 Inprise Corporation }
- { }
- {*******************************************************}
-
- unit Math;
-
- { This unit contains high-performance arithmetic, trigonometric, logorithmic,
- statistical and financial calculation routines which supplement the math
- routines that are part of the Delphi language or System unit. }
-
- {$N+,S-}
-
- interface
-
- uses SysUtils;
-
- 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;
-
- {-----------------------------------------------------------------------
- References:
-
- 1) P.J. Plauger, "The Standard C Library", Prentice-Hall, 1992, Ch. 7.
- 2) W.J. Cody, Jr., and W. Waite, "Software Manual For the Elementary
- Functions", Prentice-Hall, 1980.
- 3) Namir Shammas, "C/C++ Mathematical Algorithms for Scientists and Engineers",
- McGraw-Hill, 1995, Ch 8.
- 4) H.T. Lau, "A Numerical Library in C for Scientists and Engineers",
- CRC Press, 1994, Ch. 6.
- 5) "Pentium(tm) Processor User's Manual, Volume 3: Architecture
- and Programming Manual", Intel, 1994
-
- 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 }
-
- { Logorithmic 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;
-
- type
- EInvalidArgument = class(EMathError) end;
-
- implementation
-