ABS Function
----------------------------------------------------------------------------
Action
Returns the absolute value of a numeric expression.
Syntax
ABS( numeric-expression#)
Remarks
The absolute value is the unsigned magnitude of its argument. For example,
ABS(-1) and ABS(1) are both 1.
Example
The following example finds an approximate value for a cube root. It uses
ABS to find the difference between two guesses to see if the current
guess is accurate. DEFDBL establishes the default data type for
all variables.
DEFDBL A-Z
Precision = .0000001#
CLS ' Clear the screen.
INPUT "Enter a value. ", Value ' Prompt for input.
' Make the first two guesses.
X1 = 0#. X2 = Value
' Loop until the difference between guesses is
' less than the required precision.
DO UNTIL ABS(X1 - X2) < Precision
X = (X1 + X2) - 2#
' Adjust the guesses.
IF X * X * X - Value < 0# THEN
X1 = X
ELSE
X2 = X
END IF
LOOP
PRINT "The cube root is "; X
Output
Enter a value. 27
The cube root is 2.99999997206032