SmartPascal
Abs
Function
Gives the absolute value of a number (-ve sign is removed) System unit
 function Abs ( Number : Numeric type ) : Numeric type;
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
 
Example code : Illustrating absolute values of different data types
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

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;
 
end.
Hide full unit code
   Abs(float) = 1.5
   Abs(bigFloat) = INF
   Abs(int) = 7
   Abs(varVar) = 98