Thread: Problem with cos, sin fuctions

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Problem with cos, sin fuctions

    My codes need to use cos, sin function, but while I was coding it, it occurred to me that the cos and sin functions weren't returning the right values. I then wrote a small testing codes as such:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main (int argc, char ** argv)
    {
    
    
    	double v, x;
    	x = 0;
    
    	v = cos(x);
    	printf ("%d\n", v);
    
    	return 0;
    }
    The value printed is 0... and when I put 3.14 as x, the value went to millions.

    What seems to be the problem?

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    2
    ooops nvm, I found the error >_> I was being dumb

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I don't know what's wrong, but %d is for a decimal integer. For double, you should use %lf

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by NeonBlack View Post
    I don't know what's wrong, but %d is for a decimal integer. For double, you should use %lf
    For printf(), %f is used for both float and double. For scanf(), %f is used for float, and %lf for double.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Robatino: Does the C standard say that? I've always been able to use them interchangeably with printf.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by NeonBlack View Post
    Robatino: Does the C standard say that? I've always been able to use them interchangeably with printf.
    Floats are converted to double before passing to any function UNLESS SPECIFICALLY defined by the function, so printf() which takes a variable number of arguments will always have a "double" for float - there is NO WAY to pass a float as float to printf. If there is a function prototype that specifies float, the float will stay float, of course. You can seamlessly pass float into a function that expects double without problems too - the compiler converts automagically.

    On a further note: &#37;lf is used in some versions of C to indicate "long double" - if you mix this with a regular double value, the printout will be "rubbish".

    On the other hand, scanf() takes a pointer to the data being filled in - and a pointer to a float and pointer to double are different things - a float is only half the size, so if you use %f to read a double, only half the data will be filled in [with the wrong format!] and even worse if you have a float and specifly %lf - you overwrite whatever is after that float with the second half of double [and again, the first half has the wrong content, because double and float have different internal formats].

    --
    Mats
    Last edited by matsp; 11-16-2007 at 03:19 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by NeonBlack View Post
    Robatino: Does the C standard say that? I've always been able to use them interchangeably with printf.
    I'm not sure about the Standard, but K&R2 (page 244) and the following link seem to indicate that l is not for floating-point types:

    http://www.cplusplus.com/reference/c...io/printf.html

    while the following link seems to indicate that it can be:

    http://www.cppreference.com/stdio/printf.html

    Can someone else please clear this up?

    Edit: Thank you matsp, took too long to write this.
    Last edited by robatino; 11-16-2007 at 03:31 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > On a further note: &#37;lf is used in some versions of C to indicate "long double" - if you mix this with a regular
    > double value, the printout will be "rubbish".

    According to K&R2 and the first link above, one uses the length modifier L to indicate long double with printf().
    Last edited by robatino; 11-16-2007 at 03:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Quad equation using fuctions problem
    By godfrey270487 in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2006, 11:23 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Trigonometry Sin rule calculation problem
    By Harry in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2006, 12:14 PM