# Delphi Math Unit function

Source: Internet
Author: User
Tags integer division

This unit contains high-performance arithmetic, trigonometric, logarithmic, statistical and financial calculations and FPU program functions to complement the mathematical routines in the Delphi language System.pas unit
Attention
1) For some functions, the concepts or constants involved in this unit are provided by Earl F. Glynn (www.efg2.com) and Ray Lischner (www.tempest-sw.com).
2) The results of all the trigonometric functions of angular parameters are expressed in radians.
3) Most of the following triangular and logarithmic programs map directly to the Intel 80387 FPU
4) floating point machine instruction, input domain, output range and error handling are mainly determined by the FPU hardware.
5) code in assembler supports Pentium FPU pipeline system
& #61548; Constant
The range of IEEE floating-point types, including informal
1) minsingle = 1.5e-45; Minimum single number
2) Maxsingle = 3.4e+38; Maximum single number
3) mindouble = 5.0e-324; Minimum double number
4) maxdouble = 1.7e+308; Maximum double number
5) minextended = 3.4e-4932; Minimum extended number
6) maxextended = 1.1e+4932; Maximum number of extended
7) Mincomp = -9.223372036854775807e+18; Minimum comp number
8) Maxcomp = 9.223372036854775807e+18; Max Comp number

The following constants should not be used for comparison relationships, only for allocation. Use the isNaN and isinfinity functions to compare relationships. (provided at the back)
9) NaN = 0.0/0.0; Non-number (* \$EXTERNALSYM nan*) (* \$HPPEMIT ' static const Extended NaN = 0.0/0.0; ' *)
Ten) Infinity = 1.0/0.0; Positive Infinity (* \$EXTERNALSYM infinity*) (* \$HPPEMIT ' static const Extended Infinity = 1.0/0.0; ' *)
One) neginfinity = -1.0/0.0; Negative Infinity (* \$EXTERNALSYM neginfinity*) (* \$HPPEMIT ' static const Extended neginfinity = -1.0/0.0; ' *)
One or three corner function
1) function Arccos (const x:extended): Extended; (In: | X| &lt;= 1 out: [0..PI] radians)
2) function ArcSin (const x:extended): Extended; (In: | X| &lt;= 1 out: [-PI/2..PI/2] radians)
3) function ArcTan2 (const Y, x:extended): Extended; In: | y| &lt; 2^64, | X| &lt; 2^64, X &lt;&gt; 0 out: [-pi. (PI] radians). Calculates ArcTan (y/x) and returns an angle within the correct quadrant
4) Process Sincos (const theta:extended; var Sin, cos:extended) register;
Sincos: twice times faster than calling sin and cos to calculate the same angle separately
5) function Tan (const x:extended): Extended;
6) function Cotan (const x:extended): Extended; {1/tan (x), x &lt;&gt; 0}
7) function secant (const x:extended): Extended; {1/cos (X)}
8) function cosecant (const x:extended): Extended; {1/sin (X)}
9) function Hypot (const X, y:extended): Extended; {Sqrt (x**2 + y**2)}
Second, the Angle unit conversion program
1) function radtodeg (const radians:extended): Extended; {degrees: = radians * 180/PI}
4) function Degtorad (const degrees:extended): Extended; {radians: = degrees * pi/180}
5) function Degtograd (const degrees:extended): Extended;
6) function degtocycle (const degrees:extended): Extended;
10) function Cycletorad (const cycles:extended): Extended; {radians: = circumference * 2PI}
11) function cycletodeg (const cycles:extended): Extended;
12) function Cycletograd (const cycles:extended): Extended;
Tri-curve function
1) radian Cot (const x:extended): Extended; {alias = Cotan}
2) function Sec (const x:extended): Extended; {alias = secant}
3) function Csc (const x:extended): Extended; {alias = cosecant}
4) function Cosh (const x:extended): Extended;
5) function Sinh (const x:extended): Extended;
6) function Tanh (const x:extended): Extended;
7) function Coth (const x:extended): Extended;
8) function SecH (const x:extended): Extended;
9) function Csch (const x:extended): Extended;
10) function Arccot (const x:extended): Extended; {in:x &lt;&gt; 0}
11) function arcsec (const x:extended): Extended; {in:x &lt;&gt; 0}
12) function ARCCSC (const x:extended): Extended; {in:x &lt;&gt; 0}
13) function Arccosh (const x:extended): Extended; {in:x &gt;= 1}
14) function Arcsinh (const x:extended): Extended;
15) function Arctanh (const x:extended): Extended; {in: | X| &lt;= 1}
16) function Arccoth (const x:extended): Extended; {in:x &lt;&gt; 0}
17) function Arcsech (const x:extended): Extended; {in:x &lt;&gt; 0}
18) function Arccsch (const x:extended): Extended; {in:x &lt;&gt; 0}
Four, logarithmic function
1) function LnXP1 (const x:extended): Extended; {Ln (x + 1), accurate for x near zero}
2) function Log10 (const x:extended): Extended; {x logarithm of cardinality 10}
3) function Log2 (const x:extended): Extended; {x logarithm of cardinality 2}
4) function Logn (const Base, x:extended): Extended; {x logarithm of cardinality n}
Five, exponential function
1) function intpower (const base:extended; const Exponent:integer): Extended register;
Intpower: The exponent integer power of any base base. Fast
2) function Power (const Base, exponent:extended): Extended;
Power: Any power of any cardinality; for analysed by Sub or | exponents | &gt; MaxInt, base must &gt; 0.
Vi. various Routines
1) Process frexp (const x:extended; var mantissa:extended; var exponent:integer) register;
Frexp: Separating the mantissa and exponent of X
2) function ldexp (const x:extended; const P:integer): Extended register;
3) function ceil (const x:extended): Integer;
Ceil: Minimum integer &gt;= X, | X| &lt; MaxInt
4) function floor (const x:extended): Integer;
Floor: Maximum integer &lt;= X, | X| &lt; MaxInt
5) function Poly (const x:extended; const Coefficients:array of Double): Extended;
Ploy: Computes the unary polynomial a[0] + a[1]*x + ... + a[n]*x^n the value when the variable is X.
VII. statistical functions
For these statistical and fiscal functions, ordinary business table macro names are already written in the comments behind each function
1) function Mean (const data:array of Double): Extended;
Arithmetic mean (avg): sum/n; SUM (data)/(High (data)-Low (data) + 1)
2) function Sum (const Data:array of double): Extended register; sum of Double numbers}
3) function Sumint (const data:array of Integer): integer register, integer number and
4) function sumofsquares (const data:array of Double): extended;double squared sum
5) Process Sumsandsquares (const data:array of double; var sum, sumofsquares:extended) register; returns Double sum, squared, and
6) function MinValue (const Data:array of Double): double; Returns the minimum number in a double array (min)
7) function Minintvalue (const data:array of Integer): integer; Returns the smallest number in an integer array (min)
8) function Min (const A, B:integer): Integer; overload; Returns the smallest integer
9) function Min (const A, b:int64): Int64; overload; Returns the minimum Int64 number
10) function Min (const A, b:single): single; overload; Returns the minimum single number
11) function Min (const A, b:double): Double; overload; Returns the minimum double number
12) function Min (const A, b:extended): Extended; overload; Returns the minimum extended number
13) function MaxValue (const Data:array of Double): double; Returns the maximum number of double arrays (max)
14) function Maxintvalue (const data:array of Integer): integer; Returns the maximum number of integer arrays
15) function Max (const A, B:integer): Integer; overload; Returns the maximum number of integers
16) function Max (const A, b:int64): Int64; overload; Returns the maximum number of Int64
17) function Max (const A, b:single): single; overload; Returns the maximum single number
18) function Max (const A, b:double): Double; overload; Returns the maximum number of double
19) function Max (const A, b:extended): Extended; overload; Returns the maximum number of extended
20) function StdDev (const data:array of Double): Extended;
Sample standard deviation std; Sqrt (Variance (Data))
21) Process Meanandstddev (const data:array of Double; var Mean, stddev:extended);
Computes the arithmetic mean (Mean) and the standard deviation (StdDev) in a single call
22) function Popnstddev (const data:array of Double): Extended;
Overall standard deviation; Sqrt (Popnvariance (Data))
23) function Variance (const data:array of Double): Extended;
Sample variance (sample variance); totalvariance (data)/(high data)
24) function popnvariance (const data:array of Double): Extended;
Population variance: (VAR or VARP): totalvariance/n}; totalvariance (data)/(High (data)-Low (data) + 1)
25) function totalvariance (const data:array of Double): Extended;
Summation of variance: SUM (I=1,n) [(X (i)-Mean) **2]; SUMSQUARES-SQR (SUM)/(High (data)-Low (data) + 1)
26) function Norm (const data:array of Double): Extended;
Euclid l2-Norm sqrt (sumofsquares)}
27) Process Momentskewkurtosis (const data:array of Double; var M1, M2, M3, M4, Skew, kurtosis:extended);
Momentskewkurtosis:: Calculating the core factor of statistical decomposition
M1: Average (Mean)
M2: Variance (Variance)
Skew: Symmetry of the reflected distribution [M3/(m2** (3/2))]
Kurtosis: Reflecting the flatness of the distribution [M4/SQR (M2)]
28) function Randg (Mean, stddev:extended): Extended;
RANDG: Generates a random number, random number in accordance with the normal distribution near the mean number, for simulating data sampling error
Viii. common/promiscuous functions
1. Extreme testing:
1) function IsNan (const avalue:double): Boolean; overload;
For example infinity, Nan Double value has 7FF exponent, and Nan value has a fractional field of not 0
2) function IsNan (const avalue:single): Boolean; overload;
3) function IsNan (const avalue:extended): Boolean; overload;
4) function isinfinite (const avalue:double): Boolean;
For example NaN, a infinity Double value has a 7FF exponent, but the Infinity value has a 0 fractional field (with a fraction field of 0). Infinity value can be specified as positive or negative at the highest sign bit
2. Simple symbol Test
Type
Tvaluesign = -1..1;
Const
Negativevalue = Low (tvaluesign);
Zerovalue = 0;
Positivevalue = High (tvaluesign);

1) function sign (const avalue:integer): tvaluesign; overload;
2) function sign (const AVALUE:INT64): tvaluesign; overload;
3) function sign (const avalue:double): tvaluesign; overload;

3. Floating-point comparison
Comparefloat and Samefloa) if Epsilon is not given or is 0, it will attempt to calculate the comparison with an error that matches the current floating-point type.
1) function CompareValue (const A, b:extended; epsilon:extended = 0): tvaluerelationship; overload;
2) function CompareValue (const A, b:double; epsilon:double = 0): tvaluerelationship; overload;
3) function CompareValue (const A, b:single; Epsilon:single = 0): tvaluerelationship; overload;
4) function CompareValue (const A, B:integer): tvaluerelationship; overload;
5) function CompareValue (const A, b:int64): tvaluerelationship; overload;
6) function Samevalue (const A, b:extended; epsilon:extended = 0): Boolean; overload;
7) function Samevalue (const A, b:double; epsilon:double = 0): Boolean; overload;
8) function Samevalue (const A, b:single; Epsilon:single = 0): Boolean; overload;

4.0 Test
Iszero: Returns "true" if the value is 0 (or very very close to 0)
1) function Iszero (const a:extended; epsilon:extended = 0): Boolean; overload;
2) function Iszero (const a:double; epsilon:double = 0): Boolean; overload;
3) function Iszero (const a:single; Epsilon:single = 0): Boolean; overload;
5. Easy-to-use conditional functions
1) function Ifthen (avalue:boolean; const Atrue:integer; const Afalse:integer = 0): Integer; overload;
2) function Ifthen (avalue:boolean; const atrue:int64; const AFALSE:INT64 = 0): Int64; overload;
3) function Ifthen (avalue:boolean; const atrue:double; const afalse:double = 0.0): Double; overload;
6. Various random functions
1) function Randomrange (const afrom, Ato:integer): Integer;
2) function Randomfrom (const avalues:array of Integer): integer; overload;
3) function Randomfrom (const avalues:array of Int64): Int64; overload;
4) function Randomfrom (const Avalues:array of Double): double; overload;
7. Scope Test function
1) function InRange (const avalue, AMin, Amax:integer): Boolean; overload;
2) function InRange (const avalue, AMin, Amax:int64): Boolean; overload;
3) function InRange (const avalue, AMin, amax:double): Boolean; overload;
8. Range Cut-off function
1) function Ensurerange (const avalue, AMin, Amax:integer): Integer; overload;
2) function Ensurerange (const avalue, AMin, Amax:int64): Int64; overload;
3) function Ensurerange (const avalue, AMin, amax:double): Double; overload;
9.16-bit integer division One call results and remainder
1) process Divmod (Dividend:integer; Divisor:word; var Result, Remainder:word);
10. Rounding to a special number or a power of 10
Adigit in the range of 37 to 37, here are some examples
3 = 10^3 = Thousand ' s place
2 = 10^2 = Hundred ' s Place
1 = 10^1 = Ten ' s place
-1 = 10^-1 = 1/10 = Tenth ' s place
-2 = 10^-2 = 1/100 = Hundredth ' s place
-3 = 10^-3 = 1/1000 = Thousandth ' place}
Type
Troundtorange = -37..37;
1) function roundto (const avalue:double; const adigit:troundtorange): Double;
The variables of the Roundto function follow the asymmetric arithmetic rounding algorithm (if Frac (X) &lt; 0.5 returns x otherwise returns X+1). The default is rounding to 1/100.
2) function simpleroundto (const avalue:double; Const ADIGIT:TROUNDTORANGE =-2): Double;
Ix. financial functions,
Follow the Quattro Pro standard, parametric conventions concept. Monetary income is positive and monetary expenditure is negative (for example, borrowers who repay the loan are negative). Interest rate is a payment period, 11% of the annual interest rate, equivalent to each month (11/100)/12 = 0.00916667

Type
Tpaymenttime = (ptendofperiod, ptstartofperiod);
1) function doubledecliningbalance (const cost, salvage:extended; Life, Period:integer): Extended;
{Double declining Balance (DDB)}
2) function FutureValue (const rate:extended; Nperiods:integer; Const Payment, presentvalue:extended; Paymenttime:tpaymenttime): Extended;
Future value (end value (fval))
3) function interestpayment (const rate:extended; Period, Nperiods:integer; Const PresentValue, futurevalue:extended; Paymenttime:tpaymenttime): Extended;
Interest income (IPAYMT)
4) function interestrate (nperiods:integer; const Payment, PresentValue, futurevalue:extended; Paymenttime:tpaymenttime): Extended;
Interest rate (irate)
5) function Internalrateofreturn (const guess:extended; const Cashflows:array of Double): Extended;
Internal profitability (profit margin) (Internal rate of Return. (IRR) requires a cash flow array
6) function numberofperiods (const rate:extended; payment:extended; Const PresentValue, futurevalue:extended; Paymenttime:tpaymenttime): Extended;
Cycles {Number of Periods (NPER)}
7) function netpresentvalue (const rate:extended; const Cashflows:array of Double; Paymenttime:tpaymenttime): Extended;
Now net value, NPV (NET Present value). (NPV), a cash flow array is required
8) function Payment (rate:extended; Nperiods:integer; Const PresentValue, futurevalue:extended; Paymenttime:tpaymenttime): Extended;
Expense {Payment (PAYMT)}
9) function periodpayment (const rate:extended; Period, Nperiods:integer; Const PresentValue, futurevalue:extended; Paymenttime:tpaymenttime): Extended;
Payment Cycle {Period Payment (PPAYMT)}
10) function PresentValue (const rate:extended; Nperiods:integer; Const Payment, futurevalue:extended; Paymenttime:tpaymenttime): Extended;
Present value {Present value (PVAL)}
11) function slndepreciation (const cost, salvage:extended; Life:integer): Extended;
Linear depreciation {Straight line depreciation (SLN)}
12) function syddepreciation (const cost, salvage:extended; Life, Period:integer): Extended;
Depreciation {sum-of-years-digits depreciation (SYD)}
X. FPU exception/accuracy/rounding Management
The following function allows you to control the FPU behavior. Control FPU exception handling, FPU default precision setting, how the FPU rounds control
Type
Tfpuroundingmode = (rmnearest, Rmdown, Rmup, rmtruncate);
1) function Getroundmode:tfpuroundingmode; Returns the current rounding mode
2) function Setroundmode (const roundmode:tfpuroundingmode): Tfpuroundingmode;
Sets the new rounding mode and returns the old mode

Type
Tfpuprecisionmode = (Pmsingle, pmreserved, pmdouble, pmextended);
3) function Getprecisionmode:tfpuprecisionmode; Returns the current precision control mode
4) function Setprecisionmode (const precision:tfpuprecisionmode): Tfpuprecisionmode;
Set a new precision control mode and return to the old mode

Type
Tfpuexception = (Exinvalidop, exdenormalized, Exzerodivide,
Exoverflow, Exunderflow, exprecision);
Tfpuexceptionmask = set of tfpuexception;
Any element setting in the mask prevents the FPU from producing various exceptions. It attempts to return a best value, often nan or infinity. Numeric dependent operation and current rounding mode
5) function Getexceptionmask:tfpuexceptionmask; Returns the exception mask from the control word
Sets a new exception mask and returns the old
7) Process clearexceptions (Raisepending:boolean = True); Clears any undefined exceptions in the status word

Delphi Math Unit function

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.