There are so many good things in Delphi, so many that they make people feel annoying. This feeling is when I played the game "Hero Invincible 3". After I changed the money, I had to build buildings every day when I had money. It was obviously a good thing, but it made people feel annoying.
Record it first, and then come back to strengthen your research on Math units. There is no need to invent functions to compare floating point numbers by yourself~
Ceil
function Ceil(const X: Extended):Integer;: Round a variable in the positive infinity direction. For example:
Ceil(-2.8)=-2;Ceil(2.8)=3;Ceil(-1.0)=-1;Floor
function Floor(const X: Extended): Integer;: Round a variable in the negative infinity direction. For example:
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;
Compare the relationship between the two variables A and B. If A<B, the return value is -1; if A=B, the return value is 0; if A>B, the return value is 1; among which A and B can only be Integer, Int64, Single, Double, Extended expression.
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 ;
Returns a value that is guaranteed to be within a certain range. If AValue<AMin, return AMin; if AValue>AMax, return AMax; its return value can only be values of Integer, Int64, and Double type.
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 ;
Used to determine whether a number is within a certain range. If AMin<=AValue<=AMax, return True; otherwise, return 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;
Compare the two numeric expressions to return the larger one. Among them, A and B are types of Integer, Int64, Single, Double, and 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;
Comparing the two numeric expressions returns the smaller of them. Among them, A and B are types of Integer, Int64, Single, Double, and Extended.
7. Power, Round, RoundTo
Power
function Power(const Base, Exponent: Extended): Extended;: Returns any power of the base number. where base is the base and Exponent is the exponent.
Round
function Round(X: Extended): Int64;: Round the real number into an integer.
RoundTo
type TRoundToRange = -37..37;function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;: Round the real number according to the specified ADigit.
RoundTo(1234567,3)=1234000; RoundTo(1.234,-2)=1.23; RoundTo(1.235,-2)=1.24;8.Trunc
function Trunc(X: Extended): Int64;: Returns the integer part of a function. Similar to the Int function.
The functions described above are more commonly used in the Math class.