delpjhi的Math.pas一个公共单元,是对常规数学例程的补充,它是Delphi语言或系统单元的一部分。它包含高性能的算术、三角函数、对数函数、统计、财务计算和FPU例程。
常量定义
常量定义了IEEE范围的浮点类型,包括非正规值,共计20个
MinSingle = 1.1754943508222875080e-38;//最小单精度数
MinSingleDenormal = 1.4012984643248170709e-45 platform;//非正规最小单精度数
MaxSingle = 340282346638528859811704183484516925440.0;//最大单精度数
MinDouble = 2.2250738585072013831e-308;//最小双精度数
MinDoubleDenormal = 4.9406564584124654418e-324 platform;//非正规最小双精度数
MaxDouble = 1.7976931348623157081e+308;//最大双精度数
MinExtended80 = 3.36210314311209350625e-4932;//最小扩展精度80位
MinExtended80Denormal = 3.64519953188247460253e-4951 platform;//非正规最小扩展精度80位
MaxExtended80 = 1.18973149535723176505e+4932;//最大扩展精度80位
MinExtended = MinExtended80 platform;//最小扩展精度
MinExtendedDenormal = MinExtended80Denormal platform;//非正规最小扩展精度
MinExtended = MinDouble platform;
MinExtendedDenormal = MinDoubleDenormal platform;
MaxExtended = MaxExtended80 platform;
MaxExtended = MaxDouble platform;
MinComp = -9223372036854775807;//最小计算类型数
MaxComp = 9223372036854775807;//最大计算类型数
{ 下面的常量不应该被用于比较关系,只能用于赋值。对于比较关系请使用下面提供的IsNan和IsInfinity函数。}
NaN = 0.0 / 0.0; //无穷小
Infinity = 1.0 / 0.0;//正无穷大
Neg Infinity = -1.0 / 0.0;//负无穷大
各种数学函数
(一)三角函数
{ 输入: |X| <= 1 输出: [0..PI] 弧度 }
function ArcCos(const X : Extended) : Extended; overload; //
function ArcCos(const X : Double) : Double; overload;
function ArcCos(const X : Single) : Single; overload;
{ 输入: |X| <= 1 输出: [-PI/2..PI/2] 弧度 }
function ArcSin(const X : Extended) : Extended; overload;
function ArcSin(const X : Double) : Double; overload;
function ArcSin(const X : Single) : Single; overload;
{ ArcTan2 计算 ArcTan(Y/X), 并返回一个正确象限中的角度; 输入: |Y| < 2^64, |X| < 2^64, X <> 0 输出: [-PI..PI] 弧度 }
function ArcTan2(const Y, X: Extended): Extended; overload;
function ArcTan2(const Y, X: Double): Double; overload;
function ArcTan2(const Y, X: Single): Single; overload;
{ SinCos比分别调用Sin和Cos计算同一个角度快2倍 }
procedure SinCos(const Theta: Single; var Sin, Cos: Single); overload;
procedure SinCos(const Theta: Double; var Sin, Cos: Double); overload;
procedure SinCos(const Theta: Extended; var Sin, Cos: Extended); overload;
function Tan(const X: Single): Single; overload;
function Tan(const X: Double): Double; overload;
function Tan(const X: Extended): Extended; overload;
function Cotan(const X: Single): Single; overload; { 1 / tan(X), X <> 0 }
function Cotan(const X: Double): Double; overload;
function Cotan(const X: Extended): Extended; overload;
function Secant(const X: Single): Single; overload; { 1 / cos(X) }
function Secant(const X: Double): Double; overload;
function Secant(const X: Extended): Extended; overload;
function Cosecant(const X: Single): Single; overload; { 1 / sin(X) }
function Cosecant(const X: Double): Double; overload;
function Cosecant(const X: Extended): Extended; overload;
function Hypot(const X, Y: Single): Single; overload;
function Hypot(const X, Y: Double): Double; overload;
function Hypot(const X, Y: Extended): Extended; overload; { Sqrt(X**2 + Y**2) }
(二 )角度单位转换例程
function RadToDeg(const Radians: Single): Single; inline; overload; { 度 := 弧度 * 180 / PI }
function RadToDeg(const Radians: Double): Double; inline; overload;
function RadToDeg(const Radians: Extended): Extended; inline; overload;
function RadToGrad(const Radians: Single): Single; inline; overload; { 梯度 := 弧度 * 200 / PI }
function RadToGrad(const Radians: Double): Double; inline; overload;
function RadToGrad(const Radians: Extended): Extended; inline; overload;
function RadToCycle(const Radians: Single): Single; inline; overload; { Cycles := 弧度 / 2PI }
function RadToCycle(const Radians: Double): Double; inline; overload;
function RadToCycle(const Radians: Extended): Extended; inline; overload;
function DegToRad(const Degrees: Single): Single; inline; overload; { 弧度 := 度 * PI / 180}
function DegToRad(const Degrees: Double): Double; inline; overload;
function DegToRad(const Degrees: Extended): Extended; inline; overload;
function DegToGrad(const Degrees: Single): Single; overload;
function DegToGrad(const Degrees: Double): Double; overload;
function DegToGrad(const Degrees: Extended): Extended; overload;
function DegToCycle(const Degrees: Single): Single; overload;
function DegToCycle(const Degrees: Double): Double; overload;
function DegToCycle(const Degrees: Extended): Extended; overload;
{ 当角度不在-180和180之间 remove left }
function DegNormalize(const Degrees: Single): Single; inline; overload;
function DegNormalize(const Degrees: Double): Double; inline; overload;
function DegNormalize(const Degrees: Extended): Extended; inline; overload;
function GradToRad(const Grads: Single): Single; inline; overload; { 弧度 := Grads * PI / 200 }
function GradToRad(const Grads: Double): Double; inline; overload;
function GradToRad(const Grads: Extended): Extended; inline; overload;
function GradToDeg(const Grads: Single): Single; overload;
function GradToDeg(const Grads: Double): Double; overload;
function GradToDeg(const Grads: Extended): Extended; overload;
function GradToCycle(const Grads: Single): Single; overload;
function GradToCycle(const Grads: Double): Double; overload;
function GradToCycle(const Grads: Extended): Extended; overload;
function CycleToRad(const Cycles: Single): Single; inline; overload; { 弧度 := Cycles * 2PI }
function CycleToRad(const Cycles: Double): Double; inline; overload;
function CycleToRad(const Cycles: Extended): Extended; inline; overload;
function CycleToDeg(const Cycles: Single): Single; overload;
function CycleToDeg(const Cycles: Double): Double; overload;
function CycleToDeg(const Cycles: Extended): Extended; overload;
function CycleToGrad(const Cycles: Single): Single; overload;
function CycleToGrad(const Cycles: Double): Double; overload;
function CycleToGrad(const Cycles: Extended): Extended; overload;
(三)双曲线函数和其逆函数
function Cot(const X: Single): Single; inline; overload; { 别名Cotan }
function Cot(const X: Double): Double; inline; overload;
function Cot(const X: Extended): Extended; inline; overload;
function Sec(const X: Single): Single; inline; overload; { 别名Secant }
function Sec(const X: Double): Double; inline; overload;
function Sec(const X: Extended): Extended; inline; overload;
function Csc(const X: Single): Single; inline; overload; { 别名Cosecant }
function Csc(const X: Double): Double; inline; overload;
function Csc(const X: Extended): Extended; inline; overload;
function Cosh(const X: Single): Single; overload;
function Cosh(const X: Double): Double; overload;
function Cosh(const X: Extended): Extended; overload;
function Sinh(const X: Single): Single; overload;
function Sinh(const X: Double): Double; overload;
function Sinh(const X: Extended): Extended; overload;
function Tanh(const X: Single): Single; overload;
function Tanh(const X: Double): Double; overload;
function Tanh(const X: Extended): Extended; overload;
function CotH(const X: Single): Single; inline; overload;
function CotH(const X: Double): Double; inline; overload;
function CotH(const X: Extended): Extended; inline; overload;
function SecH(const X: Single): Single; inline; overload;
function SecH(const X: Double): Double; inline; overload;
function SecH(const X: Extended): Extended; inline; overload;
function CscH(const X: Single): Single; inline; overload;
function CscH(const X: Double): Double; inline; overload;
function CscH(const X: Extended): Extended; inline; overload;
function ArcCot(const X: Single): Single; overload; { 输入: X <> 0 }
function ArcCot(const X: Double): Double; overload;
function ArcCot(const X: Extended): Extended; overload;
function ArcSec(const X: Single): Single; overload; { 输入: X <> 0 }
function ArcSec(const X: Double): Double; overload;
function ArcSec(const X: Extended): Extended; overload;
function ArcCsc(const X: Single): Single; overload; { 输入: X <> 0 }
function ArcCsc(const X: Double): Double; overload;
function ArcCsc(const X: Extended): Extended; overload;
function ArcCosh(const X: Single): Single; overload; { 输入: X >= 1 }
function ArcCosh(const X: Double): Double; overload;
function ArcCosh(const X: Extended): Extended; overload;
function ArcSinh(const X: Single): Single; overload;
function ArcSinh(const X: Double): Double; overload;
function ArcSinh(const X: Extended): Extended; overload;
function ArcTanh(const X: Single): Single; overload; { 输入: |X| <= 1 }
function ArcTanh(const X: Double): Double; overload;
function ArcTanh(const X: Extended): Extended; overload;
function ArcCotH(const X: Single): Single; overload; { 输入: X <> 0 }
function ArcCotH(const X: Double): Double; overload;
function ArcCotH(const X: Extended): Extended; overload;
function ArcSecH(const X: Single): Single; overload; { 输入: X <> 0 }
function ArcSecH(const X: Double): Double; overload;
function ArcSecH(const X: Extended): Extended; overload;
function ArcCscH(const X: Single): Single; overload; { 输入: X <> 0 }
function ArcCscH(const X: Double): Double; overload;
function ArcCscH(const X: Extended): Extended; overload;
(四)对数函数
function LnXP1(const X: Single): Single; overload; { Ln(X + 1), X接近于零的精确值 }
function LnXP1(const X: Double): Double; overload;
function LnXP1(const X: Extended): Extended; overload;
function Log10(const X: Single): Single; overload; { Log以10为底X的对数 }
function Log10(const X: Double): Double; overload;
function Log10(const X: Extended): Extended; overload;
function Log2(const X: Single): Single; overload; { Log以2为底X的对数 }
function Log2(const X: Double): Double; overload;
function Log2(const X: Extended): Extended; overload;
function LogN(const Base, X: Single): Single; overload; { Log以N为底X的对数 }
function LogN(const Base, X: Double): Double; overload;
function LogN(const Base, X: Extended): Extended; overload;
(五)指数(幂)函数
{ IntPower: 提高Base为一个整数幂. 快. }
function IntPower(const Base: Single; const Exponent: Integer): Single; overload;
function IntPower(const Base: Double; const Exponent: Integer): Double; overload;
function IntPower(const Base: Extended; const Exponent: Integer): Extended; overload;
{ Power: 提高Base为任意幂。 对于分数Exponent(指数), or |exponents| > MaxInt, Base必须> 0. }
function Power(const Base, Exponent: Extended): Extended; overload;
function Power(const Base, Exponent: Double): Double; overload;
function Power(const Base, Exponent: Single): Single; overload;
(六)杂项例程
{ Frexp: 浮点数分解函数-分离X的尾数和指数 }
procedure Frexp(const X: Single; var Mantissa: Single; var Exponent: Integer); overload;
procedure Frexp(const X: Double; var Mantissa: Double; var Exponent: Integer); overload;
procedure Frexp(const X: Extended; var Mantissa: Extended; var Exponent: Integer); overload;
{ Ldexp: 返回 X*(2^P) }
function Ldexp(const X: Single; const P: Integer): Single; overload;
function Ldexp(const X: Double; const P: Integer): Double; overload;
function Ldexp(const X: Extended; const P: Integer): Extended; overload;
{ Ceil: 向正无穷方向取整-返回大于或者等于X的最小整数 >= X, |X| < MaxInt }
function Ceil(const X: Single): Integer; overload;
function Ceil(const X: Double): Integer; overload;
function Ceil(const X: Extended): Integer; overload;
{ Floor: 返回比X小的最大整数:返回小于等于X的最大整数 <= X, |X| < MaxInt }
function Floor(const X: Single): Integer; overload;
function Floor(const X: Double): Integer; overload;
function Floor(const X: Extended): Integer; overload;
{ Poly: 求一个变量X的统一多项式的值。 这个系数Coefficients随着X的幂的递增顺序排列:
Coefficients[0]*(X^0) + Coefficients[1]*(X^1) + ... + Coefficients[N]*(X**N) }
function Poly(const X: Single; const Coefficients: array of Single): Single; overload;
function Poly(const X: Double; const Coefficients: array of Double): Double; overload;
function Poly(const X: Extended; const Coefficients: array of Extended): Extended; overload;
(七)统计函数
在每个函数之前的注释中给出了这些统计和财务函数的通用商业电子表格宏名称。{ Mean: Data算术平均值。 (AVG): SUM / N }
function Mean(const Data: array of Single): Single; overload;
function Mean(const Data: array of Double): Double; overload;
function Mean(const Data: array of Extended): Extended; overload;
{ Sum: 求Data的和。 (SUM) }
function Sum(const Data: array of Single): Single; overload;
function Sum(const Data: array of Double): Double; overload;
function Sum(const Data: array of Extended): Extended; overload;
function SumInt(const Data: array of Integer): Integer;
{函数SumOfSquares:Data的平方和。}
function SumOfSquares(const Data: array of Single): Single; overload;
function SumOfSquares(const Data: array of Double): Double; overload;
function SumOfSquares(const Data: array of Extended): Extended; overload;
{过程SumsAndSquares:同时返回Data的和Sum与平方和SumOfSquares }
procedure SumsAndSquares(const Data: array of Single; var Sum, SumOfSquares: Extended); overload;
procedure SumsAndSquares(const Data: array of Double; var Sum, SumOfSquares: Extended); overload;
procedure SumsAndSquares(const Data: array of Extended; var Sum, SumOfSquares: Extended); overload;
{ MinValue: 返回数据数组Data中最小的有符号值 (MIN) }
function MinValue(const Data: array of Single): Single; overload;
function MinValue(const Data: array of Double): Double; overload;
function MinValue(const Data: array of Extended): Extended; overload;
function MinIntValue(const Data: array of Integer): Integer;
{ Min: 返回A、B中最小值 (MIN) }
function Min(const A, B: Integer): Integer; overload; inline;
function Min(const A, B: Int64): Int64; overload; inline;
function Min(const A, B: UInt64): UInt64; overload; inline;
function Min(const A, B: Single): Single; overload; inline;
function Min(const A, B: Double): Double; overload; inline;
function Min(const A, B: Extended): Extended; overload; inline;
{ MaxValue: 返回数据数组中最大的有符号值 (MAX) }
function MaxValue(const Data: array of Single): Single; overload;
function MaxValue(const Data: array of Double): Double; overload;
function MaxValue(const Data: array of Extended): Extended; overload;
function MaxIntValue(const Data: array of Integer): Integer;
{ Max: 返回A、B中最大值 (MIN) }
function Max(const A, B: Integer): Integer; overload; inline;
function Max(const A, B: Int64): Int64; overload; inline;
function Max(const A, B: UInt64): UInt64; overload; inline;
function Max(const A, B: Single): Single; overload; inline;
function Max(const A, B: Double): Double; overload; inline;
function Max(const A, B: Extended): Extended; overload; inline;
{ Standard Deviation (STD): Sqrt(Variance). StdDev即样本标准差 }
function StdDev(const Data: array of Single): Single; overload;
function StdDev(const Data: array of Double): Double; overload;
function StdDev(const Data: array of Extended): Extended; overload;
{ MeanAndStdDev 在一次调用中计算Mean(s算术平均值)和StdDev(标准差) }
procedure MeanAndStdDev(const Data: array of Single; var Mean, StdDev: Single); overload;
procedure MeanAndStdDev(const Data: array of Double; var Mean, StdDev: Double); overload;
procedure MeanAndStdDev(const Data: array of Extended; var Mean, StdDev: Extended); overload;
{ population standard deviation(PopnStdDev总体标准偏差) (STDP): Sqrt(PopnVariance). 用于一些商业和财务计算。 }
function PopnStdDev(const Data: array of Single): Single; overload;
function PopnStdDev(const Data: array of Double): Double; overload;
function PopnStdDev(const Data: array of Extended): Extended; overload;
{ Variance (VARS): TotalVariance / (N-1). Variance即样本方差 }
function Variance(const Data: array of Single): Single; overload;
function Variance(const Data: array of Double): Double; overload;
function Variance(const Data: array of Extended): Extended; overload;
{ population variance(PopnVariance总体方差) (VAR or VARP): TotalVariance/ N }
function PopnVariance(const Data: array of Single): Single; overload;
function PopnVariance(const Data: array of Double): Double; overload;
function PopnVariance(const Data: array of Extended): Extended; overload;
{ total variance(TotalVariance总方差,总变异数): SUM(i=1,N)[(X(i) - Mean)**2] }
function TotalVariance(const Data: array of Single): Single; overload;
function TotalVariance(const Data: array of Double): Double; overload;
function TotalVariance(const Data: array of Extended): Extended; overload;
{ Norm: 欧几里得L2-范数 Sqrt(SumOfSquares) }
function Norm(const Data: array of Single): Single; overload;
function Norm(const Data: array of Double): Double; overload;
function Norm(const Data: array of Extended): Extended; overload;
{ MomentSkewKurtosis: 计算统计分析的核心因子:
前四阶矩加上偏度和峰度系数。
M1是Mean(算术平均数). M2是Variance(方差).
Skew反映了分布的对称性: M3 / (M2**(3/2))
Kurtosis反映了分布的平直度: M4 / Sqr(M2) }
procedure MomentSkewKurtosis(const Data: array of Double; var M1, M2, M3, M4, Skew, Kurtosis: Extended);
{ RandG产生高斯分布(即正态分布)于Mean附近的的随机数。 用于模拟带有抽样误差的数据。}
function RandG(Mean, StdDev: Single): Single; overload;
function RandG(Mean, StdDev: Double): Double; overload;
function RandG(Mean, StdDev: Extended): Extended; overload;
-
(八)常规/杂项 使用函数
{(1) 极限测试 }
// 像一个infinity, 一个NaN双精度值有一个7FF的指数, 但是NaN值有一个不为0的分数域。
function IsNan(const AValue: Single): Boolean; overload;
function IsNan(const AValue: Double): Boolean; overload;
function IsNan(const AValue: Extended): Boolean; overload;
// 像一个NaN, 一个infinity双精度值有一个7FF的指数,但是infinity值有一个为0的分数域。
// Infinity值可以为正也可为负,是在高位的符号位中被指定的
function IsInfinite(const AValue: Single): Boolean; overload;
function IsInfinite(const AValue: Double): Boolean; overload;
function IsInfinite(const AValue: Extended): Boolean; overload;
{(2) 简单的符号测试}
type TValueSign = -1..1;const NegativeValue = Low(TValueSign); ZeroValue = 0; PositiveValue = High(TValueSign);function Sign(const AValue: Integer): TValueSign; overload;function Sign(const AValue: Int64): TValueSign; overload;function Sign(const AValue: Single): TValueSign; overload;function Sign(const AValue: Double): TValueSign; overload;function Sign(const AValue: Extended): TValueSign; overload;{ CompareFloat & SameFloat: 如果Epsilon没有给定(或为零),我们将尝试根据使用的浮点类型的精度来计算一个合理的值。 }function CompareValue(const A, B: Extended; Epsilon: Extended = 0): TValueRelationship; overload;function CompareValue(const A, B: Double; Epsilon: Double = 0): TValueRelationship; overload;function CompareValue(const A, B: Single; Epsilon: Single = 0): TValueRelationship; overload;function CompareValue(const A, B: Integer): TValueRelationship; overload;function CompareValue(const A, B: Int64): TValueRelationship; overload;function CompareValue(const A, B: UInt64): TValueRelationship; overload;function SameValue(const A, B: Extended; Epsilon: Extended = 0): Boolean; overload;function SameValue(const A, B: Double; Epsilon: Double = 0): Boolean; overload;function SameValue(const A, B: Single; Epsilon: Single = 0): Boolean; overload;{ IsZero: 如果给定值是0(或者是非常非常接近于它),将返回True。}function IsZero(const A: Extended; Epsilon: Extended = 0): Boolean; overload;function IsZero(const A: Double; Epsilon: Double = 0): Boolean; overload;function IsZero(const A: Single; Epsilon: Single = 0): Boolean; overload;
{ (3) 易于使用的条件函数 }
function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer; overload; inline;function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0): Int64; overload; inline;function IfThen(AValue: Boolean; const ATrue: UInt64; const AFalse: UInt64 = 0): UInt64; overload; inline;function IfThen(AValue: Boolean; const ATrue: Single; const AFalse: Single = 0.0): Single; overload; inline;function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0): Double; overload; inline;function IfThen(AValue: Boolean; const ATrue: Extended; const AFalse: Extended = 0.0): Extended; overload; inline;/// <summary>FMod返回ANumerator / ADenominator的余数 (像整数中的mod),其符号与ANumerator相同。</summary>function FMod(const ANumerator, ADenominator: Single): Single; overload;function FMod(const ANumerator, ADenominator: Double): Double; overload;function FMod(const ANumerator, ADenominator: Extended): Extended; overload;
{(4) 各种随机函数 }
function RandomRange(const AFrom, ATo: Integer): Integer;function RandomFrom(const AValues: array of Integer): Integer; overload;function RandomFrom(const AValues: array of Int64): Int64; overload;function RandomFrom(const AValues: array of UInt64): UInt64; overload;function RandomFrom(const AValues: array of Single): Single; overload;function RandomFrom(const AValues: array of Double): Double; overload;function RandomFrom(const AValues: array of Extended): Extended; overload;
{(5) 区间范围测试函数 }
function InRange(const AValue, AMin, AMax: Integer): Boolean; overload; inline;function InRange(const AValue, AMin, AMax: Int64): Boolean; overload; inline;function InRange(const AValue, AMin, AMax: UInt64): Boolean; overload; inline;function InRange(const AValue, AMin, AMax: Single): Boolean; overload; inline;function InRange(const AValue, AMin, AMax: Double): Boolean; overload; inline;function InRange(const AValue, AMin, AMax: Extended): Boolean; overload; inline;
{(6) 区间范围截断函数 }
function EnsureRange(const AValue, AMin, AMax: Integer): Integer; overload;function EnsureRange(const AValue, AMin, AMax: Int64): Int64; overload;function EnsureRange(const AValue, AMin, AMax: UInt64): UInt64; overload;function EnsureRange(const AValue, AMin, AMax: Single): Single; overload;function EnsureRange(const AValue, AMin, AMax: Double): Double; overload;function EnsureRange(const AValue, AMin, AMax: Extended): Extended; overload;
{(7) 16位无符号整数除法及其余数 }
procedure DivMod(Dividend: Cardinal; Divisor: Word; var Result, Remainder: Word); overload;{ 64 位无符号整数除法及其余数 }procedure DivMod(Dividend: UInt64; Divisor: UInt64; var Result, Remainder: UInt64); overload;
{(8) 四舍五入到一个特定的数字或10的幂 }
{ ADigit的有效范围是37到-37。 这里是一些有效的ADigit值的例子... 3 = 10^3 = 1000 = 千位 2 = 10^2 = 100 = 百位 1 = 10^1 = 10 = 十位 -1 = 10^-1 = 1/10 = 十分之一位 -2 = 10^-2 = 1/100 = 白分之一位 -3 = 10^-3 = 1/1000 = 千分之一位}type TRoundToRange = -37..37;type TRoundToEXRangeExtended = -20..20;function RoundTo(const AValue: Extended; const ADigit: TRoundToEXRangeExtended): Extended;{ 这个RoundTo函数的变量遵循对称的算术舍入算法 (如果Frac(X) < .5那么返回X,否则: 如果X > 0则返回X + 1; 如果X < 0则返回X - 1。) 这个函数默认四舍五入到百分位(分)。 注意:FPU四舍五入模式影响该函数的行为。 }function SimpleRoundTo(const AValue: Single; const ADigit: TRoundToRange = -2): Single; overload;function SimpleRoundTo(const AValue: Double; const ADigit: TRoundToRange = -2): Double; overload;function SimpleRoundTo(const AValue: Extended; const ADigit: TRoundToRange = -2): Extended; overload;(九)财务函数。
参数约定:从A的角度来看, A接受金额为正,A支付金额为负(例如。 借款人贷款的还款,对借款人来说是负的。).利率是指每个支付期。11%的年贷款利率,每年支付12次,每次支付的利率应当是(11 / 100) / 12 = 0.00916667
type
TPaymentTime = (ptEndOfPeriod, ptStartOfPeriod);
{ Double Declining Balance (DDB) 双倍余额递减法}
function DoubleDecliningBalance(const Cost, Salvage: Extended;
Life, Period: Integer): Extended;
{ Future Value (FVAL)终值 }
function FutureValue(const Rate: Extended; NPeriods: Integer; const Payment,
PresentValue: Extended; PaymentTime: TPaymentTime): Extended;
{ Interest Payment (IPAYMT) 利息支付 }
function InterestPayment(const Rate: Extended; Period, NPeriods: Integer;
const PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;
{ Interest Rate (IRATE) 利率}
function InterestRate(NPeriods: Integer; const Payment, PresentValue,
FutureValue: Extended; PaymentTime: TPaymentTime): Extended;
{ Internal Rate of Return. (IRR)内部收益率。需要大量现金流。 }
function InternalRateOfReturn(const Guess: Extended;
const CashFlows: array of Double): Extended;
{ Number of Periods (NPER)期数 }
function NumberOfPeriods(const Rate: Extended; Payment: Extended;
const PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;
{ Net Present Value. (NPV)净现值。需要大量现金流。 }
function NetPresentValue(const Rate: Extended; const CashFlows: array of Double;
PaymentTime: TPaymentTime): Extended;
{ Payment (PAYMT) 年金付款}
function Payment(Rate: Extended; NPeriods: Integer; const PresentValue,
FutureValue: Extended; PaymentTime: TPaymentTime): Extended;
{ Period Payment (PPAYMT)定期付款 }
function PeriodPayment(const Rate: Extended; Period, NPeriods: Integer;
const PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;
{ Present Value (PVAL)现值 }
function PresentValue(const Rate: Extended; NPeriods: Integer;
const Payment, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;
{ Straight Line depreciation (SLN) 直线折旧 直线折旧法}
function SLNDepreciation(const Cost, Salvage: Extended; Life: Integer): Extended;
{ Sum-of-Years-Digits depreciation (SYD) 年数总和折旧法}
function SYDDepreciation(const Cost, Salvage: Extended; Life, Period: Integer): Extended;
(十)FPU/SSE异常/精度/舍入管理
下面的函数允许您控制FPU/SSE的行为。
有了这些,你就可以控制一个FPU/SSE异常, 如何使用缺省精度,最后,如何通过FPU/SSE处理四舍五入。type
TRoundingMode = (rmNearest, rmDown, rmUp, rmTruncate);
{$IFNDEF NEXTGEN}
TFPURoundingMode = type TRoundingMode;
TSSERoundingMode = type TRoundingMode;
{$ENDIF !NEXTGEN}
{ 返回当前的舍入模式 }
{$IFNDEF NEXTGEN}
function GetFPURoundMode: TFPURoundingMode; platform;
function GetSSERoundMode: TSSERoundingMode; platform;
{$ENDIF !NEXTGEN}
function GetRoundMode: TRoundingMode;
{ 设置舍入模式并返回旧模式 }
{$IFNDEF NEXTGEN}
function SetFPURoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode; platform;
function SetSSERoundMode(const RoundMode: TSSERoundingMode): TSSERoundingMode; platform;
{$ENDIF !NEXTGEN}
function SetRoundMode(const RoundMode: TRoundingMode): TRoundingMode;
{$IFNDEF NEXTGEN}
type
TFPUPrecisionMode = (pmSingle, pmReserved, pmDouble, pmExtended) platform;
{ 返回当前的精度控制模式 }
function GetPrecisionMode: TFPUPrecisionMode; platform;
{ 设置精度控制模式并返回旧的模式 }
function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode; platform;
{$ENDIF !NEXTGEN}
type
TArithmeticException = (exInvalidOp, exDenormalized, exZeroDivide,
exOverflow, exUnderflow, exPrecision);
{$IFNDEF NEXTGEN}
TFPUException = type TArithmeticException;
TSSEException = type TArithmeticException;
{$ENDIF !NEXTGEN}
const
exAllArithmeticExceptions = [
TArithmeticException.exInvalidOp,
TArithmeticException.exDenormalized,
TArithmeticException.exZeroDivide,
TArithmeticException.exOverflow,
TArithmeticException.exUnderflow,
TArithmeticException.exPrecision];
DefaultExceptionFlags = [
TArithmeticException.exInvalidOp,
TArithmeticException.exZeroDivide,
TArithmeticException.exOverflow];
type
TArithmeticExceptions = set of TArithmeticException;
TArithmeticExceptionMask = set of TArithmeticException;
{$IFNDEF NEXTGEN}
TFPUExceptionMask = set of TFPUException;
TSSEExceptionMask = set of TSSEException;
{$ENDIF !NEXTGEN}
{18 从控制字中返回异常掩码。
掩码中的任何元素设置防止FPU出现各种异常。
相反,它以一个值返回最佳尝试,通常是NaN或infinity(无穷大)。
这个值取决于操作和当前的舍入模式。 }
{$IFNDEF NEXTGEN}
function GetFPUExceptionMask: TFPUExceptionMask; platform;
function GetSSEExceptionMask: TSSEExceptionMask; platform;
{$ENDIF !NEXTGEN}
function GetExceptionMask: TArithmeticExceptionMask;
{ 设置一个新的异常掩码并返回旧的 }
{$IFNDEF NEXTGEN}
function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask; platform;
function SetSSEExceptionMask(const Mask: TSSEExceptionMask): TSSEExceptionMask; platform;
{$ENDIF !NEXTGEN}
function SetExceptionMask(const Mask: TArithmeticExceptionMask): TArithmeticExceptionMask;
{ 清除状态位中任何挂起的异常位}
{$IFNDEF NEXTGEN}
procedure ClearFPUExceptions(RaisePending: Boolean = True); platform;
procedure ClearSSEExceptions(RaisePending: Boolean = True); platform;
{$ENDIF !NEXTGEN}
procedure ClearExceptions(RaisePending: Boolean = True; ExceptionFlags: TArithmeticExceptions = DefaultExceptionFlags);
{ 获得累积的浮点异常标志 }
function GetExceptions: TArithmeticExceptions;
{ 设置累积浮点异常标志并返回旧的}
function SetExceptions(const Exceptions: TArithmeticExceptions): TArithmeticExceptions;
{ 产生累积浮点异常 }
procedure RaiseExceptions(const ExceptionFlags: TArithmeticExceptions = DefaultExceptionFlags);
{$IFNDEF NEXTGEN}
{ 为System.TestSSE变量 的SSE类型}
const
seSSE = $0001;
seSSE2 = $0002;
seSSE3 = $0004;
seSSSE3 = $0008;
seSSE41 = $0010;
seSSE42 = $0020;
sePOPCNT = $0040;
seAESNI = $0080;
sePCLMULQDQ = $0100;
{$ENDIF !NEXTGEN}