qb45advr.hlp (Topic list)
ABS Function Programming Example
  QuickSCREEN      Details     Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
ABS Function Programming 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 enough.
 
DEFDBL A-Z
Precision = .0000001#
CLS                               'Clear the screen
INPUT "Enter a value: ", Value    'Prompt for input
'  Make the first two guesses.
X1 = 0.0# : X2 = Value
'  Go until the difference between two guesses is
'  less than the required precision.
DO UNTIL ABS(X1 - X2) < Precision
    X = (X1 + X2) / 2.0#
    ' Adjust the guesses.
    IF X * X * X - Value < 0.0# THEN
        X1 = X
    ELSE
        X2 = X
    END IF
LOOP
PRINT "The cube root is "; X
 
Sample Output
 
Enter a value: 27
The cube root is  2.999999972060323