![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 1
| My only problem is how I can do the alternating plus minus on each term. This is the code I currently have in which I can't still figure out if it is right or wrong. double cosine(double x) { int n, ctr; double cos; cos = 0; n = 2*NUMBER_OF_TERMS; ctr = NUMBER_OF_TERMS; while(ctr > 0) { ctr--; cos = cos + (RaiseTo(x, n)/factorial(n)); cos *= -1; n -= 2; } return cos; } *Note: We were only allowed to use stdio.h as the header file. |
| potsky is offline | |
| | #2 |
| Registered User Join Date: Dec 2006
Posts: 1,780
| Code: if (term % 2 == 0) {
x *= -1;
}
Code: double sign = 1;
while (...) {
x *= sign;
sign *= -1;
}
|
| cyberfish is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| x *= -1 is also the same as x = -x. The latter would probably be faster in terms of instructions, but a good compiler will probably optimize it. However, be sure to read this: << !! Posting Code? Read this First !! >>
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| c programming, taylor's series, trigonometry |
| Thread Tools | |
| Display Modes | |
|