Nth root of

I wrote a program to calculate the nth root of a number up to two decimal places. for example, the 4th root of 81 is 3., the 3rd root of 125 is 5. It works fine, except for the second root of 4. It gives an output of 1.99 instead of 2. Here is the code.

#include<stdio.h> int main(int argc, char **argv) { double root1(int,int); int n; int num1; double root; printf("\n\n-----------This is the programme to find the nth root of a number-----------\n\n"); printf("Enter a nuber greater then 1 : "); scanf("%d",&num1); if(num1>1) { printf("Enter the value for 'n'(the root to be calculated) : "); scanf("%d",&n); root = root1(num1,n); printf("%d th Root of %d is %f\n\n", n,num1,root); } else printf("wrong entry"); return 0; } double root1(int a, int b) { int j; double i,k; double incre = 0.01; for(i=1; i<=a; i = i+incre) { for(j=0;j<b;j++) { k=k*i; } if(a<k) { return(i-incre); break; } else k=1; } } 

I tried this for a few hours but can't fix it. can anyone debug this? I'll be very grateful.

+6
c algorithm floating-accuracy
source share
9 answers

You need to read "What Every Computer Scientist Should Know About Floating-Point Arithmetic . "

Floating-point numbers, which are commonly used to represent non-integer numbers, are essentially limited. These limitations provide good performance, but at the cost of such anomalies.

+10
source share

The answer, like most floating point problems, is that C works with limited precision. And the floats are binary. They cannot accurately represent the decimal number 1.99 - it will probably be a close value, for example 1.990000000023....

Standard reference for these problems: What every computer scientist needs to know about floating point

There, fortunately, is a simple solution (but not perfect!). Find the root (num * 10000.0) using unit increments. It will, of course, be 100 times more than you want. Therefore, the last two digits are the “decimal places” that you wanted. You will find that the root of 40000.0 is exactly 200.0. This works because 1.0 can be nicely presented.

The price you pay for accuracy for this purpose is that you lose it at the other end - multiplying by 10000 means that you will get a loss of accuracy with higher numbers. Simple solutions are rarely without flaws, sorry.

+5
source share

This is because computers cannot process real numbers correctly.

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

+5
source share

Well, if you want 0.01 accuracy, you need to take a step of 0.005 or less, and then round. The best way is to just use pow (num1, 1 / n) :-)

+2
source share

take k = 1;

 #include<stdio.h> int main(int argc, char **argv) { double root1(int,int); int n; int num1; double root; printf("\n\n-----------This is the programme to find the nth root of a number-----------\n\n"); printf("Enter a nuber greater then 1 : "); scanf("%d",&num1); if(num1>1) { printf("Enter the value for 'n'(the root to be calculated) : "); scanf("%d",&n); root = root1(num1,n); printf("%d th Root of %d is %f\n\n", n,num1,root); } else printf("wrong entry"); return 0; } double root1(int a, int b) { int j; double i,k=1; double incre = 0.01; for(i=1; i<=a; i = i+incre) { for(j=0;j<b;j++) { k=k*i; } if(a<k) { return(i-incre); break; } else k=1; } } 
+2
source share

what MSalters said. try decreasing the incre to see how the value is gradually approaching 2.0. you may want to get higher “internal” accuracy (that is, increase) what you return and round the internal result, say, to two digits. So you can cover these rounding issues (but this is just an unverified suspicion)

+1
source share

Doubles cannot represent floating point numbers exactly. Instead, try using a decimal data type (if c has such an opinion, sorry, I can’t remember). C # has a decimal, Java has BigDecimal classes for representing floating point numbers accurately.

0
source share

A smaller incre value should work, I used 0.001, and root1 returned 2.00 for the square root of 4.

Also, if you want the answer displayed in 2 decimal places, use% .2f when you type the root.

0
source share
 #include <iostream> #include<math.h> using namespace std; int main() { double n,m; cin>>n; cin>>m; m= pow(m, (1/n)); cout<<m; return 0; } 

Why write such a huge code. This works fine until I changed double to int.

0
source share

All Articles