开发者

nth root of a number

I wrote a program to calculate nth root of a number upto 2 decimal places. eg 4th root of 81 is 3., 3rd root of 125 is 5.Its working nicely except for the 2nd root of 4. It's giving the output 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;
}

dou开发者_StackOverflowble 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 have tried it for hours, but can't rectify it. can anybody debug this?? I will be very thankful.


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

Floating point numbers—which are what is normally used to represent non-integers—are inherently limited. Those limits allow good performance but at the cost of such anomalies.


That's because computers can't handle real numbers properly.

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


The answer, as with most floating-point problems, is that C works with a limited precision. And floats are binary. They can't exactly represent the decimal number 1.99 - it will likely be a close value like 1.990000000023.....

Standard link for these problems: What Every Computer Scientist Should Know About Floating-Point

There's luckily an easy solution (but not perfect!). Find the root of (num*10000.0), using increments of one. This will be of course 100 times the root that you really want. Hence, the last two digits are the "decimal places" you wanted. You'll find that the root of 40000.0 is precisely 200.0 This works because 1.0 can be perfectly represented.

The price you pay for for precision at that end is that you lose it on the other end - multiplying by 10000 means you'll get a loss of precision with higher numbers. Easy solutions rarely come without drawbacks, sorry.


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


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;
    }
}


what MSalters said. try making incre smaller to see how the value gradually approaches 2.0. you might want to have a higher "internal" precision (ie incre) that what you return, and round the internal result to, say, 2 digits. This way you might cover those rounding problems (but it's just an untested suspicion)


Doubles cannot necessarily represent floating point numbers accurately. Try using a decimal datatype instead (if c has such a think, sorry can't remember). C# has decimal, Java has BigDecimal classes to represent floating point numbers accurately.


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 to be displayed to 2 decimal places, use %.2f when you print the root.


#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 to write such huge code.This works perfectly until i change double to int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜