Math单元详解

**************************Math 数学单元 *********************************

Delphi / Kylix 跨平台运行时( Runtime )库

Copyright (c) 1996, 2001 Borland 软件 ********************************************************************

这个单元包含高性能的算术、三角、对数、统计和金融方面的计算及 FPU 程序函数用于补充 Delphi 语言 System.pas 单元中的数学常规程序函数

参考:

1)         P.J. Plauger, "The Standard C Library", Prentice-Hall, 1992, Ch. 7. 《标准 C 程序库》

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. C/C++ 数学的运算法则对于科学家和工程师

4)         H.T. Lau, "A Numerical Library in C for Scientists and Engineers", CRC Press, 1994, Ch. 6. C 语言中的数字的程序库对于科学家和工程师

5)         "Pentium(tm) Processor User's Manual, Volume 3: Architecture and Programming Manual", Intel, 1994 Pentium(tm) 处理器使用手册 , 3 : 体系结构和程序手册

注意

1)         对于有些函数,这个单元中涉及的概念或常数由 Earl F. Glynn ( www.efg2.com ) Ray Lischner ( www.tempest-sw.com ) 提供。

2)         所有的角度参数三角函数的结果都用弧度表示

3)         下列大部分的三角和对数程序直接的映射到 Intel 80387 FPU

4)         浮点机指令、输入域、输出范围和错误处理主要由 FPU 硬件决定

5)         汇编程序中的代码支持 Pentium FPU 管线体系

l   常数

IEEE 浮点类型的范围 , 包括非正规的

1)         MinSingle =  1.5e-45; 最小 Single

2)         MaxSingle =  3.4e+38; 最大 Single

3)         MinDouble =  5.0e-324; 最小 Double

4)         MaxDouble =  1.7e+308; 最大 Double

5)         MinExtended =  3.4e-4932; 最小 Extended

6)         MaxExtended =  1.1e+4932; 最大 Extended

7)         MinComp = -9.223372036854775807e+18; 最小 Comp

8)         MaxComp =  9.223372036854775807e+18; 最大 Comp

 

下列常数不应当被用于比较关系,仅仅用于分配。若要用于比较关系请使用 IsNan IsInfinity 函数。(已提供在后面)

9)         NaN =  0.0 / 0.0; 非数 (*$EXTERNALSYM NaN*)   (*$HPPEMIT 'static const Extended NaN = 0.0 / 0.0;'*)

10)      Infinity =  1.0 / 0.0; 正无穷大   (*$EXTERNALSYM Infinity*)   (*$HPPEMIT 'static const Extended Infinity = 1.0 / 0.0;'*)

11)      NegInfinity = -1.0 / 0.0; 负无穷大   (*$EXTERNALSYM NegInfinity*)  (*$HPPEMIT 'static const Extended NegInfinity = -1.0 / 0.0;'*)

一、           三角函数

1)         函数 ArcCos(const X: Extended): Extended; IN: |X| <= 1  OUT: [0..PI] 弧度)

2)         函数 ArcSin(const X: Extended): Extended; IN: |X| <= 1 OUT: [-PI/2..PI/2] 弧度)

3)         函数 ArcTan2(const Y, X: Extended): Extended; IN: |Y| < 2^64, |X| < 2^64, X <> 0   OUT: [-PI..PI] 弧度)。计算 ArcTan(Y/X), 并且返回一个正确象限内的角度

4)         过程 SinCos(const Theta: Extended; var Sin, Cos: Extended) register

SinCos :比分别调用 Sin Cos 计算同一个角度快两倍

5)         函数 Tan(const X: Extended): Extended;

6)         函数 Cotan(const X: Extended): Extended;           { 1 / tan(X), X <> 0 }

7)         函数 Secant(const X: Extended): Extended;          { 1 / cos(X) }

8)         函数 Cosecant(const X: Extended): Extended;        { 1 / sin(X) }

9)         函数 Hypot(const X, Y: Extended): Extended;        { Sqrt(X**2 + Y**2) }

二、           角度单位换算程序

1)         函数 RadToDeg(const Radians: Extended): Extended;  { 度数 := 弧度 * 180 / PI }

2)         函数 RadToGrad(const Radians: Extended): Extended; { 梯度 := 弧度 * 200 / PI }

3)         函数 RadToCycle(const Radians: Extended): Extended;{ 圆周 := 弧度 / 2PI }

4)         函数 DegToRad(const Degrees: Extended): Extended;  { 弧度 := 度数 * PI / 180 }

5)         函数 DegToGrad(const Degrees: Extended): Extended;

6)         函数 DegToCycle(const Degrees: Extended): Extended;

7)         函数 GradToRad(const Grads: Extended): Extended;   { 弧度 := 梯度 * PI / 200 }

8)         函数 GradToDeg(const Grads: Extended): Extended;

9)         函数 GradToCycle(const Grads: Extended): Extended;

10)      函数 CycleToRad(const Cycles: Extended): Extended; { 弧度 := 圆周 * 2PI }

11)      函数 CycleToDeg(const Cycles: Extended): Extended;

12)      函数 CycleToGrad(const Cycles: Extended): Extended;

三、 双曲线函数

1)         弧度 Cot(const X: Extended): Extended;             { 别名为 Cotan }

2)         函数 Sec(const X: Extended): Extended;             { 别名为 Secant }

3)         函数 Csc(const X: Extended): Extended;             { 别名为 Cosecant }

4)         函数 Cosh(const X: Extended): Extended;

5)         函数 Sinh(const X: Extended): Extended;

6)         函数 Tanh(const X: Extended): Extended;

7)         函数 CotH(const X: Extended): Extended;

8)         函数 SecH(const X: Extended): Extended;

9)         函数 CscH(const X: Extended): Extended;

10)      函数 ArcCot(const X: Extended): Extended;          { IN: X <> 0 }

11)      函数 ArcSec(const X: Extended): Extended;           { IN: X <> 0 }

12)      函数 ArcCsc(const X: Extended): Extended;           { IN: X <> 0 }

13)      函数 ArcCosh(const X: Extended): Extended;          { IN: X >= 1 }

14)      函数 ArcSinh(const X: Extended): Extended;

15)      函数 ArcTanh(const X: Extended): Extended;          { IN: |X| <= 1 }

16)      函数 ArcCotH(const X: Extended): Extended;         { IN: X <> 0 }

17)      函数 ArcSecH(const X: Extended): Extended;          { IN: X <> 0 }

18)      函数 ArcCscH(const X: Extended): Extended;          { IN: X <> 0 }

四、           对数函数

1)         函数 LnXP1(const X: Extended): Extended; { Ln(X + 1), accurate for X near zero }

2)         函数 Log10(const X: Extended): Extended;               { 基数为 10 X 对数 }

3)         函数 Log2(const X: Extended): Extended;               { 基数为 2 X 对数 }

4)         函数 LogN(const Base, X: Extended): Extended;          { 基数为 N X 对数 }

五、           指数函数

1)         函数 IntPower(const Base: Extended; const Exponent: Integer): Extended register;

IntPower :任意基数 Base Exponent 整数幂。快

2)         函数 Power(const Base, Exponent: Extended): Extended;

Power :任意基数的任意幂; 对于分指数或 | 指数 | > MaxInt 基数必须 > 0.

六、           各种例程

1)         过程 Frexp(const X: Extended; var Mantissa: Extended; var Exponent: Integer) register;

Frexp :分离 X 的尾数和指数

2)         函数 Ldexp(const X: Extended; const P: Integer): Extended register;

Ldexp :返回 X*2^P

3)         函数 Ceil(const X: Extended):Integer;

Ceil :最小整数 >= X, |X| < MaxInt

4)         函数 Floor(const X: Extended): Integer;

Floor :最大整数 <= X,  |X| < MaxInt

5)         函数 Poly(const X: Extended; const Coefficients: array of Double): Extended;

Ploy :计算一元多项式 A[0] + A[1]*X + ... + A[N]*X^N 在变量为 X 时的值

七、           统计函数

对于这些统计的和财政函数,普通的商业表宏名字已经写在每个函数后面的注释中

1)         函数 Mean(const Data: array of Double): Extended

算术平均值 (AVG):  SUM / N SUM(Data) / (High(Data) - Low(Data) + 1)

2)         函数 Sum(const Data: array of Double): Extended register Double 数的和 (SUM) }

3)         函数 SumInt(const Data: array of Integer): Integer register Integer 数和

4)         函数 SumOfSquares(const Data: array of Double): Extended Double 平方和

5)         过程 SumsAndSquares(const Data: array of Double; var Sum, SumOfSquares: Extended) register 返回 Double 总和、平方和

6)         函数 MinValue(const Data: array of Double): Double 返回 Double 数组中最小数 (MIN)

7)         函数 MinIntValue(const Data: array of Integer): Integer 返回 Integer 数组中最小数 (MIN)

8)         函数 Min(const A, B: Integer): Integer; overload 返回最小整数

9)         函数 Min(const A, B: Int64): Int64; overload 返回最小 Int64

10)      函数 Min(const A, B: Single): Single; overload 返回最小 Single

11)      函数 Min(const A, B: Double): Double; overload 返回最小 Double

12)      函数 Min(const A, B: Extended): Extended; overload 返回最小 Extended

13)      函数 MaxValue(const Data: array of Double): Double 返回 Double 数组最大数 MAX

14)      函数 MaxIntValue(const Data: array of Integer): Integer 返回 Integer 数组最大数

15)      函数 Max(const A, B: Integer): Integer; overload 返回最大 Integer

16)      函数 Max(const A, B: Int64): Int64; overload 返回最大 Int64

17)      函数 Max(const A, B: Single): Single; overload 返回最大 Single

18)      函数 Max(const A, B: Double): Double; overload 返回最大 Double

19)      函数 Max(const A, B: Extended): Extended; overload 返回最大 Extended

20)      函数 StdDev(const Data: array of Double): Extended

样本标准偏差 STD Sqrt(Variance(Data))

21)      过程 MeanAndStdDev(const Data: array of Double; var Mean, StdDev: Extended)

在一次调用中计算算术平均值( Mean )和标准偏差 (StdDev)

22)      函数 PopnStdDev(const Data: array of Double): Extended

总体标准差; Sqrt(PopnVariance(Data))

23)      函数 Variance(const Data: array of Double): Extended

采样方差(样品方差); TotalVariance(Data) / (High(Data) - Low(Data))

24)      函数 PopnVariance(const Data: array of Double): Extended

总体方差: (VAR or VARP): TotalVariance/ N } TotalVariance(Data) / (High(Data) - Low(Data) + 1)

25)      函数 TotalVariance(const Data: array of Double): Extended;

方差总合: SUM(i=1,N)[(X(i) - Mean)**2] SumSquares - Sqr(Sum)/(High(Data) - Low(Data) + 1)

26)      函数 Norm(const Data: array of Double): Extended;

欧几里得 L2- 范数 Sqrt(SumOfSquares) }

27)      过程 MomentSkewKurtosis(const Data: array of Double; var M1, M2, M3, M4, Skew, Kurtosis: Extended);

MomentSkewKurtosis: :计算统计分解的核心因数

M1 :平均值( Mean

M2 :方差( Variance

Skew :反映分布的对称性 [M3 / (M2**(3/2))]

Kurtosis :反映分布的平坦性 [M4 / Sqr(M2)]

28)      函数 RandG(Mean, StdDev: Extended): Extended;

RandG :产生一个随机数,随机数符合正态分布于 Mean 数附近,用于模拟数据抽取样品误差

八、           普通 / 混杂函数

1.          极端测试:

1)         函数 IsNan(const AValue: Double): Boolean; overload;

比如 infinity, NaN 双精度值有 7FF 指数 , 而且 NaN 值有不为 0 的分数域

2)         函数 IsNan(const AValue: Single): Boolean; overload;

3)         函数 IsNan(const AValue: Extended): Boolean; overload;

4)         函数 IsInfinite(const AValue: Double): Boolean;

  比如 NaN, 一个 infinity 双精度值有一个 7FF 指数 , 但是 infinity 值有一个 0 分数域( have a fraction field of 0 )。 Infinity 值能够在最高符号位被指定为正数或负数

2.        简单符号测试

type

  TValueSign = -1..1;

const

  NegativeValue = Low(TValueSign);

  ZeroValue = 0;

PositiveValue = High(TValueSign);

 

1)         函数 Sign(const AValue: Integer): TValueSign; overload;

2)         函数 Sign(const AValue: Int64): TValueSign; overload;

3)         函数 Sign(const AValue: Double): TValueSign; overload;

 

3.        浮点数比较

CompareFloat SameFloa )如果 epsilon 没有给出或者是 0 ,那将试图以一个当前浮点数类型相匹配的误差来计算比较。

1)         函数 CompareValue(const A, B: Extended; Epsilon: Extended = 0): TValueRelationship; overload;

2)         函数 CompareValue(const A, B: Double; Epsilon: Double = 0): TValueRelationship; overload;

3)         函数 CompareValue(const A, B: Single; Epsilon: Single = 0): TValueRelationship; overload;

4)         函数 CompareValue(const A, B: Integer): TValueRelationship; overload;

5)         函数 CompareValue(const A, B: Int64): TValueRelationship; overload;

6)         函数 SameValue(const A, B: Extended; Epsilon: Extended = 0): Boolean; overload;

7)         函数 SameValue(const A, B: Double; Epsilon: Double = 0): Boolean; overload;

8)         函数 SameValue(const A, B: Single; Epsilon: Single = 0): Boolean; overload;

 

4.          零测试

IsZero: 若值为 0 (或者是非常非常接近于 0 )则返回“真”

1)         函数 IsZero(const A: Extended; Epsilon: Extended = 0): Boolean; overload;

2)         函数 IsZero(const A: Double; Epsilon: Double = 0): Boolean; overload;

3)         函数 IsZero(const A: Single; Epsilon: Single = 0): Boolean; overload;

5.        简单易用的条件函数

1)         函数 IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer; overload;

2)         函数 IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0): Int64; overload;

3)         函数 IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0): Double; overload;

6.        各种随机函数

1)         函数 RandomRange(const AFrom, ATo: Integer): Integer;

2)         函数 RandomFrom(const AValues: array of Integer): Integer; overload;

3)         函数 RandomFrom(const AValues: array of Int64): Int64; overload;

4)         函数 RandomFrom(const AValues: array of Double): Double; overload;

7.        范围测试函数

1)         函数 InRange(const AValue, AMin, AMax: Integer): Boolean; overload;

2)         函数 InRange(const AValue, AMin, AMax: Int64): Boolean; overload;

3)         函数 InRange(const AValue, AMin, AMax: Double): Boolean; overload;

8.        范围切断函数

1)         函数 EnsureRange(const AValue, AMin, AMax: Integer): Integer; overload;

2)         函数 EnsureRange(const AValue, AMin, AMax: Int64): Int64; overload;

3)         函数 EnsureRange(const AValue, AMin, AMax: Double): Double; overload;

9.        16 位整数除法一次调用得出结果和余数

1)         过程 DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);

10.      圆整到特殊数字或 10 的幂

ADigit 37 -37 范围之中,下面是一些示例

3 = 10^3  = 1000   = thousand's place

2 = 10^2  =  100   = hundred's place

1 = 10^1  =   10   = 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's place }

type

  TRoundToRange = -37..37;

1)         函数 RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;

RoundTo 函数的变量遵循不对称算术舍入运算法则(如果 Frac(X) < 0.5 则返回 X 否则返回 X+1 )。其缺省凑整到 1/100

2)         函数 SimpleRoundTo(const AValue: Double; const ADigit: TRoundToRange = -2): Double;

九、           金融函数,

遵循 Quattro Pro 标准 , 参数约定概念。金钱收入为正,金钱支出为负 ( 举例来说,借款人偿还贷款则借款人为负 ) 利率是有支付周期,11%的年度利率,相当于每个月 (11 / 100) / 12 = 0.00916667

 

type

  TPaymentTime = (ptEndOfPeriod, ptStartOfPeriod);

1)         函数 DoubleDecliningBalance(const Cost, Salvage: Extended; Life, Period: Integer): Extended;

{ Double Declining Balance (DDB) }

2)         函数 FutureValue(const Rate: Extended; NPeriods: Integer; const Payment, PresentValue: Extended; PaymentTime: TPaymentTime): Extended;

  未来值(终值 (FVAL )

3) 函数 InterestPayment(const Rate: Extended; Period, NPeriods: Integer; const PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

利息收入 (IPAYMT)

4) 函数 InterestRate(NPeriods: Integer; const Payment, PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

利率 (IRATE)

5) 函数 InternalRateOfReturn(const Guess: Extended; const CashFlows: array of Double): Extended;

内部盈利率(利润率)( Internal Rate of Return. (IRR) )需要现金流数组

6) 函数 NumberOfPeriods(const Rate: Extended; Payment: Extended; const PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

    周期数 { Number of Periods (NPER) }

7) 函数 NetPresentValue(const Rate: Extended; const CashFlows: array of Double; PaymentTime: TPaymentTime): Extended;

现在净价值,纯现值( Net Present Value. (NPV), 需要现金流数组

8) 函数 Payment(Rate: Extended; NPeriods: Integer; const PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

支出 { Payment (PAYMT) }

9) 函数 PeriodPayment(const Rate: Extended; Period, NPeriods: Integer; const PresentValue, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

支付周期 { Period Payment (PPAYMT) }

10)      函数 PresentValue(const Rate: Extended; NPeriods: Integer; const Payment, FutureValue: Extended; PaymentTime: TPaymentTime): Extended;

现值 { Present Value (PVAL) }

11)      函数 SLNDepreciation(const Cost, Salvage: Extended; Life: Integer): Extended;

直线折旧 { Straight Line depreciation (SLN) }

12)      函数 SYDDepreciation(const Cost, Salvage: Extended; Life, Period: Integer): Extended;

折旧 { Sum-of-Years-Digits depreciation (SYD) }

十、 FPU 异常 / 精度 / 舍入管理

以下函数允许你控制 FPU 行为。控制 FPU 异常处理, FPU 默认精度设置, FPU 怎样舍入控制

type

TFPURoundingMode = (rmNearest, rmDown, rmUp, rmTruncate);

1)         函数 GetRoundMode: TFPURoundingMode; 返回当前舍入模式

2) 函数 SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;

设置新的舍入模式并且返回旧的模式

 

type

  TFPUPrecisionMode = (pmSingle, pmReserved, pmDouble, pmExtended);

3) 函数 GetPrecisionMode: TFPUPrecisionMode; 返回当前精度控制模式

4) 函数 SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;

设置新的精度控制模式并且返回旧的模式

 

type

  TFPUException = (exInvalidOp, exDenormalized, exZeroDivide,

                   exOverflow, exUnderflow, exPrecision);

  TFPUExceptionMask = set of TFPUException;

掩码中任何元素设置防止 FPU 产生各种异常。它企图返回一个最好的数值,经常是 NaN infinity 。数值依靠运算和当前的舍入模式

5) 函数 GetExceptionMask: TFPUExceptionMask; 从控制字中返回异常掩码

6) 函数 SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;

设置新的异常掩码并返回旧的

7) 过程 ClearExceptions(RaisePending: Boolean = True); 清除状态字中任何未定的异常位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值