首页 > 编程 > Delphi > 正文

Delphi Math里的基本函数,以及浮点数比较函数

2019-11-11 05:34:03
字体:
来源:转载
供稿:网友

Delphi里的好东西太多,多到让人觉得烦。这种感觉就是当年打游戏《英雄无敌3》,改了钱以后,有钱了每天都要造建筑,明明是好事,可是让人觉得烦。

先记录下来,以后再回来加强对Math单元的研究,不必再自己发明函数去比较浮点数了~

 

Ceil

function Ceil(const X: Extended):Integer;:按正无穷大方向四舍五入一个变量。例如:

Ceil(-2.8) = -2;  Ceil(2.8) = 3;  Ceil(-1.0) = -1;  

Floor

function Floor(const X: Extended): Integer;:按负无穷方向四舍五入一个变量。例如:

Floor(-2.8) = -3;  Floor(2.8) = 2;  Floor(-1.0) = -1;  

 3. CompareValue

function CompareValue(const A, B: Integer): TValueRelationship; overload;function CompareValue(const A, B: Int64): TValueRelationship; overload;function CompareValue(const A, B: Single; Epsilon: Single = 0): TValueRelationship; overload;function CompareValue(const A, B: Double; Epsilon: Double = 0): TValueRelationship; overload;function CompareValue(const A, B: Extended; Epsilon: Extended = 0): TValueRelationship; overload;

比较A、B两个变量的关系。如果A<B,则返回值为-1;如果A=B,则返回值为0;如果A>B,则返回值为1;其中A、B只能为Integer、Int64、Single、Double、Extended表达式。

 

4. EnsureRange

function EnsureRange(const AValue, AMin, AMax: Integer): Integer; overload;function EnsureRange(const AValue, AMin, AMax: Int64): Int64; overload;function EnsureRange(const AValue, AMin, AMax: Double): Double; overload;

返回确保在某一范围内的值。如果AValue<AMin,则返回AMin;如果AValue>AMax,则返回AMax;其返回值只能为Integer、Int64、Double类型的值。

 

5. InRange

function InRange(const AValue, AMin, AMax: Integer): Boolean; overload;function InRange(const AValue, AMin, AMax: Int64): Boolean; overload;function InRange(const AValue, AMin, AMax: Double): Boolean; overload;

用来判断一个数是否在某一范围内。如AMin<=AValue<=AMax,则返回True;否则则返回False。

 

6. Max、Min

Max

function Max(A,B: Integer): Integer; overload;function Max(A,B: Int64): Int64; overload;function Max(A,B: Single): Single; overload;function Max(A,B: Double): Double; overload;function Max(A,B: Extended): Extended; overload;

比较两个数字表达式返回其中的较大者。其中A、B的类型为Integer、Int64、Single、Double、Extended中的一类。

Min

function Min(A,B: Integer): Integer; overload;function Min(A,B: Int64): Int64; overload;function Min(A,B: Single): Single; overload;function Min(A,B: Double): Double; overload;function Min(A,B: Extended): Extended; overload;

比较两个数字表达式返回其中的较小者。其中A、B的类型为Integer、Int64、Single、Double、Extended中的一类。

 

7. Power、Round、RoundTo

Power

function Power(const Base, Exponent: Extended): Extended;:返回底数的任何次幂。其中base是底数,Exponent是指数。

Round

function Round(X: Extended): Int64;:将实数四舍五入为整数。

RoundTo

type TRoundToRange = -37..37;function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;:将实数按指定的ADigit来进行四舍五入。

RoundTo(1234567,3) = 1234000;  RoundTo(1.234,-2) = 1.23;  RoundTo(1.235,-2) = 1.24;  

 

8.Trunc

function Trunc(X: Extended): Int64;:返回一个函数的整数部分。与Int函数相似。

 

以上介绍的几个函数在Math类中比较常用。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选