Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
ABS Function, SGN Function, and DEFtype Statements Example
◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
' This example finds the cube root of an input value. The ABS and
' SGN functions test whether the current guess is accurate. The
' DEFDBL statement establishes the default data type for the
' variables.
' To try this example:
' 1. Choose New Project from the File menu
' 2. Copy the code example below to the code window
' 3. Press F5 to run the example
CLS ' Clear the screen
DEFDBL A-Z
CONST Precision = .0000001#
CONST LowGuess = -1, HighGuess = 1, ExactGuess = 0
CLS ' Clear the screen
INPUT "Enter a value: ", Value ' Prompt for input
X1 = 0#: X2 = Value ' Make the first two guesses
accuracy$ = "approximate"
DO UNTIL ABS(X1 - X2) < Precision ' Loop until the difference
X = (X1 + X2) / 2# ' between guesses is less than
SELECT CASE SGN(X * X * X - Value) ' the required precision;
CASE IS = LowGuess ' adjust the guesses
X1 = X
CASE IS = HighGuess
X2 = X
CASE IS = ExactGuess
accuracy$ = "Exact"
X1 = X2
END SELECT
LOOP
PRINT "The "; accuracy$; " cube root is"; X