Richard said:
pete said:
#include <float.h>
double square_root(double n)
{
double top = n;
double bottom = 0;
double root = 0;
double diff = 1;
while(diff < -DBL_EPSILON || diff > DBL_EPSILON)
{
root = (top + bottom) / 2;
diff = n - root * root;
if(diff < -DBL_EPSILON)
{
top = root;
}
else if(diff > DBL_EPSILON)
{
bottom = root;
}
}
return root;
}
What's that function supposed to do?
Are you even *trying* ?!
/* BEGIN square_root.c output */
DBL_MIN is 2.225074e-308.
sq_rt(DBL_MIN) is 1.491668e-154.
square_root(DBL_MIN) is 1.112537e-308.
/* END square_root.c output */
/* BEGIN square_root.c */
#include<stdio.h>
#include <float.h>
double square_root(double n);
double sq_rt(double x);
int main(void)
{
puts("/* BEGIN square_root.c output */\n");
printf("DBL_MIN is %e.\n\n",
DBL_MIN);
printf("sq_rt(DBL_MIN) is %e.\n\n",
sq_rt(DBL_MIN));
printf("square_root(DBL_MIN) is %e.\n\n",
square_root(DBL_MIN));
puts("/* END square_root.c output */");
return 0;
}
double sq_rt(double x)
{
if (x > 0) {
const double a = x;
double b = x / 2 + 0.5;
do {
x = b;
b = (a / x + x) / 2;
} while (x > b);
}
return x;
}
double square_root(double n)
{
double top = n;
double bottom = 0;
double root = 0;
double diff = 1;
while(diff < -DBL_EPSILON || diff > DBL_EPSILON)
{
root = (top + bottom) / 2;
diff = n - root * root;
if(diff < -DBL_EPSILON)
{
top = root;
}
else if(diff > DBL_EPSILON)
{
bottom = root;
}
}
return root;
}
/* END square_root.c */