Eric said:
Malcolm McLean wrote On 07/24/07 16:18,:
I read only the two sqrt() replacements. The first
is bad, but at least the text says as much. The second,
supposedly superior version is broken in at least three
ways: First, it won't compile. Second, when the obvious
bug is fixed it still won't compile under C99 and won't
link under C90. And when *that* bug is fixed, a third
bug will become apparent. Sloppy workmanship.
I'm pretty sure that the assert expression
in squareroot(), was an untested afterthought.
I didn't like the arbitrary termination conditions
invlolving magic numbers like 10 iterations in squareroot(),
or 0.000001 in exponent(), or 0.00000001 in logarithm().
The exponent and logarithm functions
could have been terminated through conditions
involving DBL_EPSILON.
A Newton-Raphson square root algorithm,
can be terminated more logically:
double sq_rt(double x)
{
if (x > 0) {
const double c = x;
double y = c / 2 + 0.5;
do {
x = y;
y = (c / x + x) / 2;
} while (x > y);
}
return x;
}
Page 55 shows pi at 3.1415926535897931160
The "1160" part, is wrong.
I uploaded some math functions written
in completely portable freestanding C code, to:
http://www.mindspring.com/~pfilandr/C/fs_math/
/* BEGIN fs_math.h */
/*
** Portable freestanding code
*/
#ifndef H_FS_MATH_H
#define H_FS_MATH_H
double fs_sqrt(double x);
double fs_log(double x);
double fs_log10(double x);
double fs_exp(double x);
double fs_modf(double value, double *iptr);
double fs_fmod(double x, double y);
double fs_pow(double x, double y);
double fs_cos(double x);
/*
** C99
*/
double fs_log2(double x);
double fs_exp2(double x);
long double fs_sqrtl(long double x);
long double fs_logl(long double x);
long double fs_expl(long double x);
long double fs_cosl(long double x);
long double fs_fmodl(long double x, long double y);
#endif
/* END fs_math.h */
This is the output from fslog_10 on my machine:
/* BEGIN fs_log10.c output */
fs_log10(10000) - 4 is 0.000000e+000
fs_pow(0.0001, -0.25) - 10 is 3.552714e-015
fs_cos(DEGREES_TO_RADIANS(-3540)) - 0.5 is 9.992007e-016
fs_sqrt(2) * fs_sqrt(2) - 2 is -4.440892e-016
log10(10000) - 4 is 0.000000e+000
pow(0.0001, -0.25) - 10 is 0.000000e+000
cos(DEGREES_TO_RADIANS(-3540)) - 0.5 is -1.060133e-015
sqrt(2) * sqrt(2) - 2 is 4.440892e-016
/* END fs_log10.c output */