Thread: UDF to calculate sine

  1. #1
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62

    UDF to calculate sine

    I was asked to calculate sine using UDF.. using pow is not allowed... i did the following way...
    Code:
    #include <stdio.h>
    #include <conio.h>
    int fact(int num);
    float powe(float base, int power);
    float sine(float x);
    int main(void)
    {
        float xx,x;
        printf("Enter the angle in degree : ");
        scanf("%f",&xx);
        while(xx>=360)
            xx=xx-360;
        if (xx>=270) xx=xx-360;
        if(xx>=180 && xx<270) xx= xx-180;
        if(xx>90 && xx<180) xx=180-xx;
        x=3.141592654/180*xx;
        printf("Sin %d = %.4f",(int)xx,sine(x));
        getch();
        return 0;
    }
    
    float sine(float x)
    {
        int i;
        float sine=0;
        for(i=1;i<=10;i++)
            sine=sine+powe(-1,i+1)*(powe(x,2*i-1)/fact(2*i-1));
        return sine;
    }
    
    int fact(int num)
    {
        int i,p=1;
        for (i=1;i<=num;i++)
        {
            p=p*i;
        }
        return p;
    }
    
    float powe(float base , int power)
    {
        int i;
        float ans=1;
        for(i=0;i<power;i++)
            ans=ans*base;
        return ans;
    }
    if i dont use the codes in red, it cant calculate if the value of degree is higher... is there any other simpler way to create this function..?
    Last edited by n3cr0_l0rd; 02-26-2009 at 08:06 AM.
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  2. #2
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    sin x = x - x^3/3! + x^5/5! - x^7/7! + x^9/9!......................

    that was the formula i used to calculate sine
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure your series isn't supposed to take a radian value for the angle, rather than degrees?

    --
    Mats
    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.

  4. #4
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    the input is in degree but it has to be converted into radian for calculating the value of sine...
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Also, as an addition to what matsp said, I don't think this code even works. It will calculate 29!, which is way too high for any usual integer in C.

  6. #6
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    i checked... it works .... but still i reduced the loop count to 10 now...
    Last edited by n3cr0_l0rd; 02-26-2009 at 08:05 AM.
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine approximation Help
    By What111 in forum C Programming
    Replies: 8
    Last Post: 11-04-2007, 06:22 PM
  2. how to calculate all files in disk?
    By kirmelyte in forum C Programming
    Replies: 1
    Last Post: 04-30-2006, 02:47 PM
  3. A faster way to calculate sine function?
    By IcyDeath in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 01:17 PM
  4. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  5. Computing the sine of an angle
    By Bearcat in forum C Programming
    Replies: 1
    Last Post: 02-13-2002, 12:46 PM