Thread: There is a mistake in my calculation,Could you help me please?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    72

    Unhappy There is a mistake in my calculation,Could you help me please?

    My problem is; float numbers can't be calculated so i cant find the real value of the equation within 10^-6 tolerance.
    after .7f digits, it doesnt calculate.
    if you look my code you can understand.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    //this program calculates the one root of an polynomial,in a interval [a,b]
    //user enters the coefficients and Degree of polynomial,After program Must calculate
    //the root within +10^-6 and -10^-6 tolerance.
    
    
    
    
    
    
    float denklem_sonuc(float *cof,float kac,int degreex)//this calculates the the polinomial value.
    {                   //for example, cof={1,3,3,1} ,kac=-1, degreex=3 ,(1*x^3+3*X^2+3*x^1+1*X^0)
        float ax,sqrx;  // in the above equation -1 will be assumed as X value. and Will calculate the answer.
        float cvpx=0;
        int i,i2;
      for(i=0;i<(degreex+1);i++)
      {
        ax=kac;
        sqrx=1;
        for(i2=0;i2<i;i2++)
        {
            sqrx=ax*sqrx; //This is power of ax calculation
        }
        cvpx=cof[degreex-i]*sqrx+cvpx; //this is the total summation of the every X^ * coefficient
      }  
      printf("The Equation=%.20f\n",cvpx);     //This displays the equation value when I put 'kac' number in the equation.
    return cvpx;
    }
    
    
    
    
    
    
    int main()
    {
    int truth=1;
    int i;
    float b=0,a=0;
    int sayac=0;
    int degreex;
    int z=1;
    float sqrx=1,j=0;
    float *cof;
    float cvp1=0;
    printf("Enter polinom degree:");
    scanf("%d",&degreex);
    cof=calloc(degreex,sizeof(double));//if there is 5th degree polynomial,there will be 6 coefficients.
    for(i=0;i<(degreex+1);i++)         //Calloc creates a pointer array which i can store the coefficients in it.
    {
    printf("please enter the coefficient of X^%d:",(degreex-i));//coefficients of eq.Stored here
    scanf("%f",&cof[i]);
    }
    //for(i=0;i<(degreex+1);i++) printf("%.20f\n",cof[i]);
    printf("The lowest number of interval,  a:");
    scanf("%f",&a);
    printf("The highest number of interval, b:");
    scanf("%f",&b);
    printf("[%f,%f] Enter any key to calculate the root in this interval\n",a,b);
    getch();
    
    
    
    
    while(1) //infinite loop,i put a option if 1000 times after,the answer of equation cant converge to [1-0] break and say "no root."
    {
    
    
    j=b-denklem_sonuc(cof,b,degreex)*((b-a)/(denklem_sonuc(cof,b,degreex)-denklem_sonuc(cof,a,degreex)));
    a=b; //this is a method for approximately converges to 0 ,and finds the root with some error.
    b=j;
    sayac++;
    if((denklem_sonuc(cof,j,degreex)<pow(10,-20))&&(denklem_sonuc(cof,j,degreex)>-pow(10,-20))) {
    printf("My Root=%.20f\n",j);
    printf("The polynomial value while the root used in equation=%.20f\n",denklem_sonuc(cof,j,degreex));
    break;
    }
    if (denklem_sonuc(cof,j,degreex)<1) truth=0;
    if ((sayac>1000)&&(truth!=0))
    {
      printf("No root, in this interval.");
      break;
    }
    
    
    }
    
    
    return 0;
    }
    The roout should be -1 or +-10^-6 tolerance of -1
    maybe -0.999999 or -1.000001

    but my program finds +- 10^-2 tolerance


    Help Please.
    Attached Images Attached Images There is a mistake in my calculation,Could you help me please?-ads-z2-jpg There is a mistake in my calculation,Could you help me please?-ads-z-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    A float only has 6 decimal digits of precision, so you can't go any further than you already are.

    You could use double instead of float, since that gives you about 15 decimal digits.

    > printf("My Root=%.20f\n",j);
    Note that this does NOT print out 20 digits of precise information. Anything past 6 (or 15) is pure noise, not accurate information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    I change floats with double but this screen comes here , And i used %g
    Attached Images Attached Images There is a mistake in my calculation,Could you help me please?-ads-z3-jpg 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 04:41 PM
  2. What is my mistake ?
    By Freelander1983 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2007, 09:31 AM