Description |
The Abs function returns the absolute value of a negative or positive number. It does this by removing a negative sign, if found.
The Number can be any numeric type, and can even be a Variant, as long as it can be converted to a number. For example, a Variant set to a string '-1.23' will work fine. Always, Abs converts the Variant to an Extended floating point number prior to removing any negative sign, even if the result is an integer value.
|
| Notes | Floating point numbers can be set to extreme values, such as infinity (see the example). The Abs function simply removes the negative sign of these, so that -INF becomes INF.
| | Related commands | Div | | Performs integer division, discarding the remainder | Mod | | Performs integer division, returning the remainder |
|
Download this web site as a Windows program.
|
|
|
|
Example code : Illustrating absolute values of different data types | var
float, bigFloat : single;
int : Integer;
varVar : Variant;
begin
float := -1.5; // Small negative floating point number
bigFloat := -4.56E100;  // Infinite negative floating point number
int := -7; // Negative integer
varVar := '-98';  // Variants are converted to floating point!
ShowMessage('Abs(float) = '+FloatToStr(Abs(float)));
ShowMessage('Abs(bigFloat) = '+FloatToStr(Abs(bigFloat)));
ShowMessage('Abs(int) = '+FloatToStr(Abs(int)));
 // Variants are converted into Extended floating types
float := Abs(varVar);
ShowMessage('Abs(varVar) = '+FloatToStr(float));
end;
| Show full unit code | Abs(float) = 1.5
Abs(bigFloat) = INF
Abs(int) = 7
Abs(varVar) = 98 |
|
|