• Delphi Math单元函数


     
    **************************数学单元*********************************
    Delphi / Kylix 跨平台运行时(Runtime)库
    Copyright (c) 1996, 2001 Borland 软件********************************************************************
    这个单元包含高性能的算术、三角、对数、统计和金融方面的计算及FPU程序函数用于补充Delphi语言System.pas 单元中的数学常规程序函数

    注意
    1) 对于有些函数,这个单元中涉及的概念或常数由Earl F. Glynn (www.efg2.com) 和 Ray Lischner (www.tempest-sw.com)提供。
    2) 所有的角度参数三角函数的结果都用弧度表示
    3) 下列大部分的三角和对数程序直接的映射到Intel 80387 FPU
    4) 浮点机指令、输入域、输出范围和错误处理主要由 FPU 硬件决定
    5) 汇编程序中的代码支持Pentium FPU 管线体系
     常数
    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); 清除状态字中任何未定的异常位

  • 相关阅读:
    [Windows Azure] How to Scale a SQL Database Solution
    [Windows Azure] Monitoring SQL Database Using Dynamic Management Views
    [Windows Azure] Managing SQL Database using SQL Server Management Studio
    [Windows Azure] How to Deploy a Database to Windows Azure
    [Windows Azure] How to Create and Configure SQL Database
    [Windows Azure] Guidelines for Connecting to Windows Azure SQL Database
    [Windows Azure] Development Considerations in Windows Azure SQL Database
    [Windows Azure] Management REST API Reference
    [Windows Azure] Windows Azure SQL Database library
    [Windows Azure] Getting Started with Windows Azure SQL Data Sync
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2664903.html
Copyright © 2020-2023  润新知