Where's the error in this Function

This is a discussion on Where's the error in this Function within the C Programming forums, part of the General Programming Boards category; I have a function that evaluates a postfix expression. It first converts the numeric digits to float and then evaluates ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    11

    Where's the error in this Function

    I have a function that evaluates a postfix expression. It first converts the numeric digits to float and then evaluates the expression.

    Problem is when it multiplies 2 floating types (e.g 3.25 and 5.7) it only gives 18.524996 instead of 18.525. same for division process.

    For addition and subtraction, it works fine. The conversion to float also works. I have debugged this one and I dont know what's the source of the problem. Hope someone could help.

    Code:
    void post(Item_c *str, int q, char t, int u)
    {
        int l2 = u;
        int i;
        int j = 0;
        float k, z, p, y,x;
        double  f;
        STACKinit_i(l2);
    	
        puts(str);
        for (i = 0; i < l2; i++)
        {
             if(isalpha(str[i]))
             {
                 printf("%f",alpha[tolower(str[i]) - 'a']);
                 system("pause");
                 STACKpush_i(alpha[tolower(str[i]) - 'a']);
             }
             if(((str[i] >= 48) && (str[i] <= 57)) || (str[i] == '.'))
             {
    	         k = str[i] - 48;
    	         while ( str[++i] != ' ' && i < l2 && str[i] != '.' && isdigit(str[i]) && str[++i] != '\t' )
    	         {	
                       k = k*10 + (str[i] - 48);	         
                       
    	         	   printf("%f", k);
    	        	   system("pause");
            	 }
    			 if (str[i] == '.')
    			 {	    
                        p = str[++i] - 48;
    					j++;
    					while ( (str[++i] != ' ') && (isdigit(str[i])) )
    	         	    {	
                            p = p*10 + (str[i] - 48);	
                            j++;         
                     	}
    	         	    f = p / pow(10 , j);
    	         	    j = 0;
    	         }
    	         STACKpush_i(k + f);
    	 }
             if(str[i] == '+')
             {
                 y = STACKpop_i();
    	         x = STACKpop_i();
    	         STACKpush_i(x+y);
             }
             if(str[i] == '-')
             {
                 y = STACKpop_i();
    	         x = STACKpop_i();
    	         STACKpush_i(x-y);
             }
             if(str[i] == '*')
             {
                 y = STACKpop_i();
    	         x = STACKpop_i();
    	         STACKpush_i(x*y);
             }
             if(str[i] == '/')
             {
                 y = STACKpop_i();
    	         x = STACKpop_i();
    	         STACKpush_i(x/y);
             }
       }
       if(q == 1)  
          {	
             alpha[tolower(t) - 'a'] = STACKpop_i();
          	 printf("%f\n", alpha[tolower(t) - 'a']);
          	 system("pause");
          
          }
       else if (q == 5)
       {
    	  printf("\nResulting value = %f\n\n", STACKpop_i());
          system("pause");
       }
    	
    	
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    That's not a problem.

    It's an inherent feature of floating point numbers. Since they're approximations, you often get a minor difference between the computed answer and the mathematical answer.

    Making the values double rather than float will help, but the fundamental feature of floating point numbers being approximations will always remain.

    > if(((str[i] >= 48) && (str[i] <= 57))
    Use character constants for better readability (and portability)
    if(((str[i] >= '0') && (str[i] <= '9'))
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    11
    I understand sir. I would just like to ask if there's any way to make my calculator more accurate than that sir?

    Its really a must for it to give accurate answers. Anyway, Thanks again for your help.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by josiah View Post
    I understand sir. I would just like to ask if there's any way to make my calculator more accurate than that sir?
    Use doubles instead of floats. It still won't be exact. That's just the way it is.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > it only gives 18.524996 instead of 18.525
    See, floats only have 6 digits of precision, so you're already pushing up against that with your 5 digit answer.
    doubles on the other hand have around 15 digits of precision, so the problem is less of an immediate problem.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    11
    Ok sir I understand now. I have read about the limit of floats which is indeed up to 6 digits only.

    Thanks again for the clarification.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21