article

Many programming languages have functions that calculate absolute values of numbers, either having the name abs or Abs. In languages such as C, it has variants for long integers and floating point numbers called labs and fabs. All the functions take a signed number as a parameter, and returns the absolute value of that number in the same datatype.

A very possible implementation of the abs functions, written in pseudo-code, would be: function abs (number n) { if n >= 0 return n; else return -n; }

Abs in different languages


Many different programming languages possess Abs functions. Their prototypes are listed as follows.

C or C++

int abs (int i); long labs (long i); float fabs (float i);

PHP

number abs (mixed n);

REALbasic

Function Abs ( d as Double ) as Double

Due to REALbasic's automatic numeric type conversion, other method signatures are not needed.

VB

Function Abs (I As Integer) Function Abs (L As Long) Function Abs (S As Single) Function Abs (D As Double)

VB.NET

Function Abs (ByVal I As Integer) Function Abs (ByVal L As Long) Function Abs (ByVal S As Single) Function Abs (ByVal D As Double)

Pascal

function abs (i: integer) :integer; function abs (l: long) :long; function abs (r: real) :real;

Languages with no Abs functions

As the Abs functions have such a simple implementation, many programming languages simply have no Abs functions. In those cases, programmers would either use an If-clause to determine negativity and calculate the absolute value, a compiler command to implement the action, or define a function using algorithms similar to that of above.

programming constructs

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Abs (programming)".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld