Thread: Sine values in integer format

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Sine values in integer format

    i am facing the problem of displaying the sine values in integer format. I am trying to create a sine look up table. Just for testing i showing 10 values. I am getting fractional part but want to convert to integer and store in an array, but not able to convert. what is the mistake

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(int argc, char const *argv[])
    {
        int index=0;
        printf("fractional values \n");
        for(index=0; index <= 10; index++)
        {
            printf("%f,\n", 1000.0*sinf(2*3.14159*index/1000));
        }
        printf("Converted to integer values \n");
        for(index=0; index <= 10; index++)
        {
            printf("%d\n",(int)1000*sinf(2*3.14159*index/1000) );
        } 
        return 0;
    }
    output is like this
    Code:
    fractional values
    0.000000,
    6.283139,
    12.566029,
    18.848424,
    25.130074,
    31.410731,
    37.690152,
    43.968081,
    50.244275,
    56.518488,
    62.790463,
    Converted to integer value
    0
    270415556
    -1928745506
    1438052426
    -2118917277
    -1456629062
    -447103537
    354588366
    1679953399
    -753718746
    -427015548

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Satya View Post
    i am facing the problem of displaying the sine values in integer format. I am trying to create a sine look up table. Just for testing i showing 10 values. I am getting fractional part but want to convert to integer and store in an array, but not able to convert. what is the mistake
    You cast 1000 to a integer (but 1000 is allready a integer).
    You include math.h, so why do you don't use the defined M_PI ?
    The line should look like this:
    Code:
    printf("%d\n",(int) (1000.0 * sinf(2 * M_PI * index / 1000.0)));
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Yes got it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 10-30-2013, 11:34 PM
  2. Replies: 6
    Last Post: 06-10-2011, 11:02 AM
  3. values of sine
    By jacek in forum C Programming
    Replies: 2
    Last Post: 10-31-2009, 04:17 PM
  4. integer format question
    By fsu_altek in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2005, 05:31 PM
  5. How to display integer to binary format?
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2005, 11:32 PM

Tags for this Thread