>This is the first time I'm using floats, mathlib, etc., so I'd really
>appreciate it if someone could send me a snippet of code or tell me:

This should do the trick for you.

Regards,
Steve Mann

# # #

#define         TT_MIN_VALUE    1.0e-6
#define         TT_ACCURACY     1.0e-6

/***********************************************************************
  * FUNCTION:           _sqrt
  *
  * DESCRIPTION:        Calculate the square root of a double number using
  *             successive approximations. The loop stops when the
  *             difference between two successive iterations is smaller
  *             than theaccuracy threshold.
  *
  * RETURNED:           The square root or zero if the number is smaller
  *             than the minimum value handled by the library.
  ***********************************************************************/

double _sqrt
(
double  x       // The number to get the square root of
)
{
        double  result = 0.0, prevResult, diff = 0.0;

// Eliminate negatives and small values outside the library range.

        if ( x >= TT_MIN_VALUE )
        {
                result = x;

// Calculate until two successive guesses are less than
// the library's minimum value. # iterations ranges from about
// 10 (for very small and very large #s) to about 5 for numbers
// close to zero.

                do
                {
                        prevResult = result;
                        result  = ( result + ( x / result )) / 2.0;
                        diff= _abs ( prevResult - result );
                }
                while ( diff > TT_ACCURACY );
        }
        return result;
}


-------------------------------------------
Creative Digital Publishing Inc.
1315 Palm Street, San Luis Obispo, CA 93401-3117
-------------------------------------------
805.784.9461              805.784.9462 (fax)
[EMAIL PROTECTED]       http://www.cdpubs.com

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to