whats wrong with this? no errors but wrong result

This is a discussion on whats wrong with this? no errors but wrong result within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> main() { double x; x =45.0; /* 45 degree */ x *= 3.141593 /180.0; /* ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    whats wrong with this? no errors but wrong result

    Code:
    #include <stdio.h>
    #include <math.h>
    main()
    {
    	double x;
    
    	x =45.0; /* 45 degree */
    	x *= 3.141593 /180.0; /* convert to radians*/
    
    	printf("The sine of 45 is:	 %f.\n", sin);
    	printf("The cosine of 45 is:	 %f.\n", cos);
    	printf("The tangent of 45 is:	 %f.\n", tan);
    	return 0;
    }
    i get no errors whatsoever but the output is only 0's, whats wrong with this?
    When no one helps you out. Call google();

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably because you aren't calling sin, cos and tan functions. You're just using the name of them. (Which is effectively a pointer to the function itself, not invoking it.) Turn on your compiler's warnings and pay attention to them. It should be freaking out.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    try sin(x) cos(x) tan(x)

    You must have warnings turned off when you compile.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    do you mean this?
    Code:
    --Configuration: trig - Win32 Debug--------------------
    Compiling...
    trig.c
    
    trig.obj - 0 error(s), 0 warning(s)
    edit: i fixed it.

    Code:
    #include <stdio.h>
    #include <math.h>
    main()
    {
    	double x;
    
    	x =45.0; /* 45 degree */
    	x *= 3.141593 /180.0; /* convert to radians*/
    
    	printf("The sine of 45 is:	 %f.\n", sin(x));
    	printf("The cosine of 45 is:	 %f.\n", cos(x));
    	printf("The tangent of 45 is:	 %f.\n", tan(x));
    	return 0;
    }
    i guess those that published the book run out of ink in this part of the code eheh
    Last edited by InvariantLoop; 01-27-2005 at 04:03 PM.
    When no one helps you out. Call google();

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. Turn your compiler's warnings on. You will get a bunch. See?
    Code:
    freakout.c:4: warning: return type defaults to `int'
    freakout.c: In function `main':
    freakout.c:10: warning: double format, pointer arg (arg 2)
    freakout.c:11: warning: double format, pointer arg (arg 2)
    freakout.c:12: warning: double format, pointer arg (arg 2)
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by InvariantLoop

    Code:
     
        x *= 3.141593 /180.0; /* convert to radians*/
    If you are going to be doing math, you might as well get the best precision that you can. Doubles are good for 15 or 16 significant decimal digits, but not if your approximation of pi is only 8 digits:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        double x;
        double pi;
    
        x =45.0; /* 45 degree */
        x *= 3.141593 /180.0; /* convert to radians*/
    
        printf("First with pi = 3.141593:\n");
        printf("  The sine of 45 degrees is:    %.15f\n", sin(x));
        printf("  The cosine of 45 degrees is:  %.15f\n", cos(x));
        printf("  The tangent of 45 degrees is: %.15f\n", tan(x));
    
        /* 
         * Here's a way to get the best precision pi that your library can
         * calculate, without typing in a bunch of digits:
         *
         */
    
        pi = 4.0 * atan(1.0);
    
        x = 45.0;
        x *= pi / 180.0;
        printf("\n\n");
        printf("Now, with pi = %.15f:\n", pi);
        printf("  The sine of 45 degrees is:    %.15f\n", sin(x));
        printf("  The cosine of 45 degrees is:  %.15f\n", cos(x));
        printf("  The tangent of 45 degrees is: %.15f\n", tan(x));
    
        return 0;
    }
    Regards,

    Dave

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > --Configuration: trig - Win32 Debug--------------------
    Then goto your project settings->compiler
    Find the warning level and set it to 4
    Then see if it complains about your code.

    Of course, the better option would be to get gcc, which as well as being a more compliant compiler than VC6, it also has many more diagnostics (in particular it can tell you when you're making a mess of printf as Quzah points out).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. need help on Calculator program in C
    By deathscyth129 in forum C Programming
    Replies: 8
    Last Post: 01-08-2005, 06:50 PM
  3. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 04:14 AM
  4. wrong result?
    By o0o in forum C++ Programming
    Replies: 5
    Last Post: 01-11-2004, 08:53 AM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

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