Abs
Belongs to : FunctionDescription
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.
Example code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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; { Abs(float) = 1.5 Abs(bigFloat) = INF Abs(int) = 7 Abs(varVar) = 98 } |