Thread: math.h question

  1. #1
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14

    math.h question

    I apologize in advance for being so new at this, and for any terminology errors. Feel free to point them out; I will not be insulted.

    I have very recently started learning C, and I am trying to write a program using math.h. I need to round a float variable to a certain number of decimal places. I was under the impression that the round() function would serve my purposes adequately, but it seems to only work for rounding to the nearest whole number.

    Does anyone know what I can do?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Search (the boards, google) for "round".

    Alternatively, are you supposed to round the value of the variable itself, or just print it out to a certain number of places? (Not the same thing.)

  3. #3
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14
    Essentially, I need to check to see if a certain variable, already defined as the quotient of two earlier variables, is a whole number. There may be a simpler method, but what I was attempting to do was to store the variable, rounded to the nearest whole number with no decimal places, as another variable, then use an if/else function to check if they are equal to one another.

    Hmmm... I think I may have just figured out a way to do it. Let me try it, and I will let you know momentarily.

    Bingo! Thanks for the help!

  4. #4
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14
    All right, I retract my bingo. I was trying to use

    while( round(k) == k)...

    to mean "while k is a whole number". That appears to put the program into an infinite loop.

    Any idea how I can restate it?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Post a more complete example of what you're trying to do.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14
    Well, the program is not very long, so I suppose I can simply post it in its entirety. Please bear with me; this is my first time using code tags, and I am not entirely sure that I am posting correctly.

    The intent of the program is to take the scanned number, q, and print its prime factors. The problem is apparently occurring in the while loop, which is intended to remove and print each factor.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    	int d = 1, q, p, e = 0;
    	float k;
    
    	scanf( "%d", &q);
    	for( d <= (sqrt(p)); ++d;) {
    		p = q;
    		k = p / d;
    		while( round(k) == k) {
    			e = d;
    			printf("%d ", e);
    			p = k;
    			k = p / d;
    		}
    	}
    	if( p == q )
    		printf( "%d ", q);
    	if( p != q && p != 1 )
    		printf( "%d ", p);
    }
    Last edited by joie0330; 06-13-2009 at 03:03 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure your for-loop isn't the infinite loop? Given that it has no way to stop until ++d equals zero.....

  8. #8
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14
    It very well could be. I just realized my "greater than or equal to" sign should be "less than or equal to". I am editing, but the same problem continues to occur.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) main returns an int.
    2) 'p' is uninitialized
    3) Your for-loop is set up incorrectly. It should be: for( /*initialization*/; /*comparison*/; /*increment*/ )
    4) The sense of the for-loop comparison should be reversed, eg: loop until 'd' reaches sqrt(q) (note: not 'p'!)
    5) Fix those problems, and then print the value of each variable each iteration of the loop so that you can spot the many logic errors in the program.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14
    Thank you. I believe I can rectify all errors, save #1. To what does that refer?

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> To what does that refer?

    The C standard mandates that 'main' return an int. Some compilers allow you to omit it, either by extension or simply because they're broken.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14
    How do I repair the error?

    int main() ?

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You got it.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    bm8gc3BlYWsgY29tcHV0ZXI=
    Join Date
    Jun 2009
    Posts
    14
    Well, I got tired of my ineffectual attempts to fix the problem, so i rewrote it, making the variables' names a bit easier to remember, and thereby work with. However, my infinite loop problem continues. Any number that I input that bypasses the while loop returns the "prime" result, and anything else prints a continual sequence of twos.

    I believe the problem may lie in the highlighted line, but I am not sure.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    	int number, factor = 2;
    	float remain, valid = 1, test;
    
    	printf("Enter number\n");
    	scanf("%d", &number);
    	remain = number;
    
    	while(factor <= sqrt(number)) {
    		test = number / factor;
    		valid = round(test) - test;
    		if(valid == 0) {
    			remain = test;
    			printf("%d", factor);
    			remain = remain / factor;
    		}
    		else factor = factor + 1;
    	}
    
    	if(remain == number)
    		printf("prime\n");
    	if(remain != number && remain != 1)
    		printf("%.0f", remain);
    }
    Just a side note: the reason I used only letters in the first draft was that I was transposing this program from another programming language, which only allows them.
    Last edited by joie0330; 06-14-2009 at 09:41 AM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The highlighted line is not a problem. Think about what's going on for six seconds: your loop depends on factor. If your if statement is true, how does factor change?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM