Thread: Relational and logical operator combination bug

  1. #1
    Registered User
    Join Date
    Aug 2020
    Posts
    3

    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?
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,576
    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. #3
    Registered User
    Join Date
    Aug 2020
    Posts
    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. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,576
    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.

  5. #5
    Registered User
    Join Date
    Aug 2020
    Posts
    3
    I will try those recommendations. I will start with debugging.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,576
    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. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,040
    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;
        }
    }
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by jimblumberg View Post
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,416
    > 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.
    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.

  10. #10
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    311
    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
    Last edited by Structure; 08-09-2020 at 09:49 AM.
    "without goto we would be wtf'd"

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,576
    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. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,159
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical not operator
    By abhi143 in forum C Programming
    Replies: 5
    Last Post: 10-29-2017, 11:25 AM
  2. logical operator
    By acpower in forum C Programming
    Replies: 5
    Last Post: 04-16-2012, 02:43 AM
  3. logical operator !
    By scrapper in forum C Programming
    Replies: 1
    Last Post: 02-18-2005, 08:30 AM
  4. Logical Operator not working?
    By xshapirox in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2004, 05:21 AM
  5. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 04:33 PM

Tags for this Thread