# 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. ## 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. 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'))

3. 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. Originally Posted by josiah
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. > 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.

6. 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.