taylor sequence in c++

Pages: 12
Jul 25, 2010 at 6:30pm
That's not a C++-related problem. The page tells you everything you need to know about fmod.

But well, since this probably ain't leading nowhere:
Add this line as the first line of your functions (you'll need to include cmath):
x=fmod(x,M_PI*2);

With that, you can reduce limit to something around 25.
Also, you need to use the double type for result and term as well, otherwise there'll be no difference.
Jul 25, 2010 at 6:52pm
many thanks man

it work correctly, for cos function it''s not that good but is sin id great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double cos(double x) 
{ 
		x=fmod(x,M_PI*2);
        double result = 1;

        int limit = 20;
        int sign = -1;
        double last=1;

        for(int i=2 ; i<limit ; i+=2,sign=-sign)
        {
          last*=(x*x)/(i*(i-1));
          result += sign*last;
        }

        return result;

} 


in case x = 1.5708000

cos(x) = -3.67321e-006

in sample run cos(x) = -0.000000

one more Q,

now no need for the second forumla, am i right?


x=fmod(x,M_PI*2);
what is M_PI??
Last edited on Jul 25, 2010 at 7:18pm
Jul 25, 2010 at 7:07pm
What do you mean, "it's not that great"?
cos(1.5708) is about -0.000003673205, which is exactly the result of the Taylor cos function.

Use the real cos function or a calculator to verify.
Jul 25, 2010 at 7:19pm
yes it's about -0.000000

but in the programme is cos(x) = -3.67321e-006 ,so it's not correct ((only in this case it shows a wrong result))

x=fmod(x,M_PI*2);
what is M_PI??
Jul 25, 2010 at 7:37pm
What are you going on about? -3.67321e-006 is equal to -0.00000367321.
M_PI is a macro constant for Pi that is defined in cmath.
Last edited on Jul 25, 2010 at 7:37pm
Jul 26, 2010 at 12:32am
i see,

thank you so much for your helping

thanks thanks

:)

Jul 26, 2010 at 12:01pm
closed account (D80DSL3A)
The second formula is the one being used in the functions here. Your recent post for the cos() implements it on line #12. Methods based on both formulas were explored in this recent post: http://www.cplusplus.com/forum/general/26540/. Mind though what Athar has said about using arguments (values for x) that the series converges for. For the trig. series being considered this is -PI to +PI. You may expect values given outside this range to produce garbage for output!
Topic archived. No new replies allowed.
Pages: 12