`
v5qqcom
  • 浏览: 1285314 次
文章分类
社区版块
存档分类
最新评论

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, 1994Pentium(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; { 基数为10X对数}

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

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

五、 指数函数

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

IntPower:任意基数BaseExponent整数幂。快

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 registerDouble数的和 (SUM) }

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

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

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); 清除状态字中任何未定的异常位

分享到:
评论

相关推荐

    Delphi Math单元函数.doc

    Delphi Math单元函数.docDelphi Math单元函数.docDelphi Math单元函数.docDelphi Math单元函数.docDelphi Math单元函数.doc

    JavaScript之Math对象(源代码)

    JavaScript之Math对象(源代码)JavaScript之Math对象(源代码)JavaScript之Math对象(源代码)JavaScript之Math对象(源代码)JavaScript之Math对象(源代码)JavaScript之Math对象(源代码)JavaScript之Math对象...

    【Java编程教程】详解 Java Math 类.pdf

    【Java编程教程】详解 Java Math 类

    math3d.exe

    math3d.exe

    Mathstudio中文教程下载

    mathstudio是手机端一款很强大的数学计算工具,这是mathstudio的中文教程,里面有函数说明。 Math Studio中文教程——内置函数全翻译 向大家推荐手机数学软件中的神器Math Studio,该软件大小只有1M,功能却强大的...

    Math对象的取整方法和获取随机数方法

    Math对象的取整方法和获取随机数方法Math对象的取整方法和获取随机数方法Math对象的取整方法和获取随机数方法Math对象的取整方法和获取随机数方法Math对象的取整方法和获取随机数方法Math对象的取整方法和获取随机数...

    Java中Arrays类与Math类详解

    主要介绍了Java中Arrays类与Math类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    commons-math3-3.5-API文档-中文版.zip

    赠送jar包:commons-math3-3.5.jar; 赠送原API文档:commons-math3-3.5-javadoc.jar; 赠送源代码:commons-math3-3.5-sources.jar; 赠送Maven依赖信息文件:commons-math3-3.5.pom; 包含翻译后的API文档:...

    python源码math.py

    python源码,math.py,math.nan acos acosh asin asinh atan atan2 ceil

    commons-math3-3.5-API文档-中英对照版.zip

    赠送jar包:commons-math3-3.5.jar; 赠送原API文档:commons-math3-3.5-javadoc.jar; 赠送源代码:commons-math3-3.5-sources.jar; 赠送Maven依赖信息文件:commons-math3-3.5.pom; 包含翻译后的API文档:...

    commons-math3-3.6.1-API文档-中文版.zip

    赠送jar包:commons-math3-3.6.1.jar; 赠送原API文档:commons-math3-3.6.1-javadoc.jar; 赠送源代码:commons-math3-3.6.1-sources.jar; 赠送Maven依赖信息文件:commons-math3-3.6.1.pom; 包含翻译后的API文档...

    math.min.js

    * math.js * https://github.com/josdejong/mathjs * * Math.js is an extensive math library for JavaScript and Node.js, * It features real and complex numbers, units, matrices, a large set of * ...

    Javascript之Math对象详解

    Math对象不同于上述的对象,它可以说是一个公共数学类,里面有很多数学方法,用于各种数学运算 但是Math对象不需要构造,对于其中的方法直接使用即可 1、常量(即属性) E 返回算术常量 e,即自然对数的底数(约...

    commons-math3-3.1.1-API文档-中英对照版.zip

    赠送jar包:commons-math3-3.1.1.jar; 赠送原API文档:commons-math3-3.1.1-javadoc.jar; 赠送源代码:commons-math3-3.1.1-sources.jar; 赠送Maven依赖信息文件:commons-math3-3.1.1.pom; 包含翻译后的API文档...

    mathpix安装包(201910月更新)

    在 Linux、macOS、Windows 系统下使用 Mathpix 可以截取到复杂数学方程式的截图,并可以转换成 LaTeX 可编辑文本,在写学术及科学文献时经常会用到。编写 LaTeX 文档最麻烦的部分是那些复杂的方程式,使用 Mathpix ...

    java 中 String format 和Math类实例详解

    主要介绍了java 中 String format 和Math类实例详解的相关资料,需要的朋友可以参考下

    commons-math3-3.6.1-API文档-中英对照版.zip

    赠送jar包:commons-math3-3.6.1.jar; 赠送原API文档:commons-math3-3.6.1-javadoc.jar; 赠送源代码:commons-math3-3.6.1-sources.jar; 赠送Maven依赖信息文件:commons-math3-3.6.1.pom; 包含翻译后的API文档...

    commons-math3-3.6.1-API文档中文版

    math3是Apache下的一款进行数学计算的一款java开源工具。 math3是一款非常好用的工具,里面提供了各种运算的方法及类,方便大家调用。 apache-commons-math3是java的一种科学计算类库,实现科学计算功能的类库其他...

Global site tag (gtag.js) - Google Analytics