Thread: About sin function of C

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

    About sin function of C

    I don't get the actual value of sine function. Such as, If i write sin(0.18) it gives 0.179030 but my calculator gives sin 0.18 = 0.0031415874.......... Can you please help how can I get the actual value. Here I also add my code.

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    	long double value;
    	value = sin(0.18);
    	printf("&#37;lf\n",value);
    	return 0;
    }
    Last edited by Salem; 08-03-2008 at 11:37 PM. Reason: Removed email address

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use the right data types, and properly inform printf() about what you're passing it. Use a double, and print it with &#37;f.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Probably because your calculator defaults to degrees, whereas the math library always uses radians.
    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.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Salem View Post
    Probably because your calculator defaults to degrees, whereas the math library always uses radians.
    I did not know this. Is there a provided function to convert it to degrees or do you have to do it by hand?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you know how trivial it is to do the conversion?
    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.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I thought it was R(180/PI)...I dunno sorry was just a question :P I dont know the standard libraries.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I usually do:
    Code:
    #define DEG_TO_RAD 0.0174532925 
    
    ...
    
    in_degrees = in_radians *= DEG_TO_RAD;
    Edit: or was it the other way around, lol.

    1 radians = 57.2957795 degrees
    Last edited by mike_g; 08-04-2008 at 07:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM