abs
Calculate absolute value. Abs returns the absolute value of a negative or positive
variable. It does this by removing a negative sign, if found. The result of the
function has the same type as its argument, which can be any numerical type.
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 a string '-1.23' will work find.
Always, abs converts the variants to an extended floating point of number prior to
removing any negative sign, even if the result is an interger value.
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
Download This Source for Free Pascal
var
floatvar, bigFloat : single;
int : Integer;
varVar : Variant;
begin
floatvar := -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!
Writeln('Abs(floatvar) = ',Abs(floatvar));
Writeln('Abs(bigFloat) = ',Abs(bigFloat));
Writeln('Abs(int) = ',Abs(int));
// Variants are converted into Extended floating types
floatvar := Abs(varVar);
Writeln('Abs(varVar) = ',floatvar);
end.
Output
Abs(floatvar) = 1.5000000000000000E+0000
Abs(bigFloat) = +Inf
Abs(int) = 7
Abs(varVar) = 9.800000000E+01
See Also
Div,
Mod,
Round,
Trunc.