Hi all,

I have written a program to find the reciprocal of an input number by using newton rhapson iterative method. Below is the algorithm implementation in C:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
double guess,newguess,input,error=1.0;                                       
printf("Enter the input number");
scanf("%f",&input);
printf("Enter the guess for the reciprocal of the input number between -1.0 and 1.0");
scanf("%f",&guess);
while(error>0.0001)
{
         newguess=guess*(2-(input*guess));
         error=newguess-guess;
         guess=newguess;
}
printf("The reciprocal of the input number is %f",newguess);
system("pause");
return 0;
}

When I enter 3 as my input number, i get an absurd answer. On using hand-calculations on paper, I am converging to the right value of the reciprocal which is .33. Kindly point out the errors. Thank you.

Dani AI

Generated

Three separate problems combine to produce the “absurd” outputs shown in the thread.

First, the I/O/type mismatch (noted by ) causes undefined behavior when a double is read with "%f" — use matching types/format specifiers (for scanf: "%lf" with double, or keep everything float). Second, the loop stopped on a signed difference instead of an absolute change (as hinted); use fabs(new - old) or better check the product error |1 - a*x|. Third—and most important—the Newton iteration
x_{n+1} = x_n(2 − a x_n)
only converges when the initial guess satisfies 0 < a·x0 < 2. Writing e_n = 1 − a xn gives e{n+1} = e_n^2, so convergence requires |e_0| < 1 (equivalently 0 < a·x0 < 2). For a = 123123 and x0 = 0.1, a·x0 = 12312.3 ≫ 2, so the next iterate jumps to −1231.03 — exactly the behaviour in Post #5.

Practical fixes and a safe sketch (not a verbatim rewrite of earlier code):

#include <math.h>
double a, x, xnew, tol = 1e-6;
scanf("%lf", &a);            /* double: use "%lf" */
if (a == 0.0) /* handle divide-by-zero */;
scanf("%lf", &x);            /* initial guess */
if (!(a*x > 0.0 && a*x < 2.0)) {
    x = 1.0 / a;             /* safe fallback initial guess */
}
do {
    xnew = x * (2.0 - a * x);
    x = xnew;
} while (fabs(1.0 - a * x) > tol);
printf("%f\n", x);

Notes: prefer double for range/precision; include <math.h> and use fabs; system("pause") is Windows-only. If division must be avoided for the initial estimate, compute a scaled approximate reciprocal from the exponent/mantissa (frexp/ldexp or a small lookup), then refine with Newton iterations.

Recommended Answers

All 4 Replies

At lines 8 and 10, use %lf (small L) instead of %f.

OR, line 6, change double to float.

Your algorithm is still producing rubbish result for 123123, guess 0.1, even when correcting the missing absolute value for error.

I have corrected my code. Here's the modified version:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float guess,newguess,input,error=1;                                       
printf("Enter the input number");
scanf("%f",&input);
printf("Enter the guess for the reciprocal of the input number between -1.0 and 1.0");
scanf("%f",&guess);
while(error>0.0001)
{
         newguess=guess*(2-(input*guess));
         error=newguess-guess;
         guess=newguess;
}
printf("The reciprocal of the input number is %f",newguess);
system("pause");
return 0;
}

This works perfectly.

Hmmm... really?

Enter the input number123123
Enter the guess for the reciprocal of the input number between -1.0 and 1.0.1
The reciprocal of the input number is -1231.030029
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.