Thread: Relational and logical operator combination bug

1. Relational and logical operator combination bug

I have written a simple program that approximates value of a Maclaurin series.

The source file is attached here because I could not make the wrap code feature work while posting.

I want to stop the while loop based on a test which goes something like this:
while( iterations < max iterations or aboslute_error < specified_error)

But the error comparison test never kicks in. However, when I remove the iterations test the error test works.

I know that it is something really fundamental that I have not taken into account. Do you think it is operator precedence that needs to be looked at ?

Can someone please point out what's going wrong?

2. Perhaps you need to assign a value to ea instead of trying to use an uninitialized variable?

Code:
```#include<stdio.h>
#include<math.h>
#include<iso646.h>

int factorial(int n);
void Maclurin(float x, float es, int maxIter);

int main(void)
{
Maclurin(1, 0.05 , 100);

return 0;
}

//Maclurin series approximation
void Maclurin(float x, float es, int maxIter)
{
//x - e^x
//es - Scarborough error value
//maxIter - maximim iterations

int iter = 1;

float oldVal = 1.0;

float val;

float ea;

while( iter < maxIter or ea > es )
{
val = oldVal + ( (pow(x, iter) )/factorial(iter) );

//error calculation
ea = fabs( ( (val - oldVal) / val) ) * 100;

oldVal = val;

iter++;

printf("%d  %.e  %f\n", iter, ea, val);
}
}
//calculate factorial
int factorial(int n)
{
int start;

int val = 1;

for( start = 1; start <= n; start++ )
val = val * start;

return val;
}```

3. In Line 28 I gave an initial value of 0 to ea. I tried that but it doesn't work. But yes, you are right. That should have solved the problem because earlier, it was making the comparison with garbage values.

I am not sure if this will help, but I am compiling the program on Windows 7 on CMD with
Code:
`gcc -std=c99 MaclurinApproximation.c -o MaclurinApproximation`

4. Have you run the program with your debugger to look at the values of the variables as you single step through the code?

Why are you using C99 instead of C11?

What version of gcc are you using?

You may want to try to use the "more normal" operator "||" to see if that makes any difference, I'm not sure if C actually supports the "or" syntax.

Also you really should be compiling with more warnings, at least -Wall and -Wextra. I also tend to use -pedantic and -pedantic-errors.

6. Also are you sure you have the correct operator?

And are you sure that your equations are correct (ie ea > es or should it be es < ea)?

7. Code:
```#include <stdio.h>
#include <math.h>

void Maclurin(float x, float es, int maxIter);

int main()
{
Maclurin(1, 0.05 , 100);
return 0;
}

//Maclurin series approximation
void Maclurin(float x, float es, int maxIter)
{
long fact = 1;
double oldVal = 1.0;

for (int iter = 1; iter <= maxIter; ++iter)
{
fact *= iter;
double val = oldVal + pow(x, iter) / fact;
double ea = fabs((val - oldVal) / val) * 100;
printf("%d  %e  %f\n", iter, ea, val);
if (ea <= es)
break;
oldVal = val;
}
}```

8. Originally Posted by jimblumberg
You may want to try to use the "more normal" operator "||" to see if that makes any difference, I'm not sure if C actually supports the "or" syntax.
"or" is a macro (expanding to ||) defined in <iso646.h> and part of c99 and c11 (Section 7.9 Alternative spellings) at least. That said, I'm not sure of the rationale behind those alternative spellings and don't use them myself

9. > while( iter < maxIter or ea > es )
Perhaps you wanted 'and' rather than 'or'

Remember that 'and(&&)' and 'or(||)' are short-circuit evaluated.
If the left of || is true, the right isn't even evaluated.

ea > es isn't even going to be tested until iter < maxIter is false.

10. Code:
`while( iter < maxIter or ea > es ) { }`
compile error: wtf is or
expected:
Code:
`while( (iter < maxIter) || (ea > es) ) { }`
Operators in C and C++ - Wikipedia

11. Did you even bother to read the previous posts?

Also if you're getting an error you probably need to update your compiler and make sure it is using one of the modern standards.

12. Structure doesn't even bother to read the Wikipedia articles they cite: under "Syntax", that Wikipedia article states for logical OR:
a || b
a or b