Just wondering if anyone could help.
I have written this code and can't seem to complete the program. It should allow a small tolerance to the program. About 0.0001%. Sorry if this seems muddled, my brain is fried.
Code:
/* Calculating cube roots using a converging approximation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LIMIT 0.0001
int near_equal(double, double);
double cube_root(double);
int
main(int argc, char **argv)
{
double x, y;
printf("Enter a value: ");
if(scanf("%lf", &x) !=1) {
printf("No value entered.\n");
exit(EXIT_FAILURE);
}
y = cube_root(x);
printf(" cube root etc etc)
return 0;
}
int near_equal(double x, double y)
{
double scaled_limit;
if(fabs(x) >= fabs(y)) {
scaled_limit = LIMIT*fabs(x);
} else {
scaled_limit = LIMIT*fabs(y);
}
#define CUBE_LOWER 1e-6
#define CUBE_UPPER 1e+6
#define CUBE_ITERATIONS 25
double
cube_root(double v) {
double next=1.0
int i;
if (fabs(v)<CUBE_LOWER || fabs(v)>CUBE_UPPER) {
printf("Warning: cube root may be inaccurate\n");
}
for (i=0; i<CUBE_ITERATIONS; i++) {
next = (2*next + v/next/next)/3;
}
return next;
}
Any help would be greatly appreciated.
Thanks.