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類中比較常用。