Delphi Math里的基本函數,以及浮點數比較函數


Delphi里的好東西太多,多到讓人覺得煩。這種感覺就是當年打游戲《英雄無敵3》,改了錢以后,有錢了每天都要造建築,明明是好事,可是讓人覺得煩。

先記錄下來,以后再回來加強對Math單元的研究,不必再自己發明函數去比較浮點數了~

 

Ceil

function Ceil(const X: Extended):Integer;:按正無窮大方向四舍五入一個變量。例如:

[delphi]  view plain copy
 
  1. Ceil(-2.8) = -2;  
  2. Ceil(2.8) = 3;  
  3. Ceil(-1.0) = -1;  

Floor

function Floor(const X: Extended): Integer;:按負無窮方向四舍五入一個變量。例如:

[delphi]  view plain copy
 
  1. Floor(-2.8) = -3;  
  2. Floor(2.8) = 2;  
  3. 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來進行四舍五入。


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

 

8.Trunc

function Trunc(X: Extended): Int64;:返回一個函數的整數部分。與Int函數相似。

 

以上介紹的幾個函數在Math類中比較常用。

 

參考:

http://blog.csdn.net/kimifdw/article/details/8582725


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM