#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    12

    A program for cos x... i thought odd.


    How would you write a program for the cos x. This is just something I overheard in class and got interested. But yet, I would have not been able to think of how to write it. Anyone know? I at least found out that the formula for cos x is 1/0! - x^2/2! + x^4/4! - x^6/6! etc. etc. etc. Although to begin with, I am a little confused on the etc. etc. etc. part with what I saw today. Anyways... to me, sounds interesting. Could I see a program maybe. Bye.
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    that's not a formula, it's an approxamation. It's been a while since my calculus days, but that looks like Taylor's. I could be confusing it with trapezoidal or something else, though.

    As for code, as a general rule we don't just right a program. We let you start it and then help you find your bugs.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    As for the etc. etc..., if you are using floats, they are only accurate to 6 decimal places if I recall correctly. I think doubles go to 14, but this is all from dim memory. In any case, whatever they are, there is no point in carrying on the approximation past the point where they flip any bits. My guess is you could just hard-code x levels of 'etc' and just be done with it. You could do one for floats and another for doubles or just one for doubles and truncate for floats.

    Generally, however, we only help those that help themselves.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    12

    Sorry


    I sowwy. I was not thinking about doing it myself first. I just hopped onto here after I remebered the thing. I have tried, and so far this is what I have come up with.

    PHP Code:
    #include <iostream> 
    #include <iostream> 

    using namespace std

    double factorial(double n); 
    double cos(double n);

    int main() 

        
    double num
        
    double answer

        
    cout << "This program will return cos and factorial of a number.\n";
        
    cout << "Enter a number: "
        
    cin >> num

        
    answer factorial(num); 

        
    cout << "Factorial of " << num << " is " << answer << '\n'

        return 
    0
       


    double factorial(double n

        
    double ans

        if(
    n==1) return 1
        
    ans factorial(n-1)*n
        return 
    ans
    }

    double cos(double n)
    {
        
    double cos_x;

        do
        {
            
    1/0(ans) - 2(ans), n+2ans+2;
        }while (
    != 0);
    }

        ...

    Getting the actual factorial of a number was not hard, but now it seems rough to be able to come up with cos. For one thing, I don't know how to write it in code, it may be obvious... but I'm not sure.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    It's a summation, so do it in for-loop.

    Actually, it's a Maclaurin series, which is a special case of a Taylor series. Look it up in a calculus textbook in the chapter on power series. Also look in a book of tables to find series for approximating several trig, exponential, and logrithmic functions. From what I was taught, computers and calculators use Maclaurin series to calculate values for those functions within our programs.

    A couple things to remember, though. The trig functions require that the angle be in radians, not degrees (1 degree = pi / 180).

    The other thing is that this is an infinite series that we're dealing with, so you have to decide when to stop. Because they converge, each term is smaller than the previous. So one technique is to decide how much error you want to allow in your answer and when you read a term that is smaller than that threshhold you stop. Or you could arbitrarily decide on a fixed number of terms to add together.

    Have fun.

IMN logo majestic logo threadwatch logo seochat tools logo