find value of cos without using math library function

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 9
Reputation: ayeshawzd is an unknown quantity at this point 
Solved Threads: 1
ayeshawzd ayeshawzd is offline Offline
Newbie Poster

find value of cos without using math library function

 
0
  #1
Oct 10th, 2009
how can i find value of cos without using math library of c#
.......plz send me code example or formula by which it ia possible i thank full to u
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,024
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 300
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic
 
0
  #2
Oct 10th, 2009
Did you search the web? Or have you absolutely no clue as how to tackle your problem?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 962
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 204
DdoubleD DdoubleD is offline Offline
Posting Shark
 
1
  #3
Oct 10th, 2009
Sent you sent me a PM, I took a quick search: http://en.wikipedia.org/wiki/Law_of_cosines

I was then going to suggest that ddanbe is your man for this question, but I see he is already on top of it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 571
Reputation: jonsca is on a distinguished road 
Solved Threads: 74
jonsca jonsca is online now Online
Posting Pro
 
0
  #4
Oct 11th, 2009
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,024
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 300
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic
 
0
  #5
Oct 13th, 2009
Because cos(x) can be writen as an infinite series. See picture included. You could do something like this :

  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. for (double d = 0; d < 3.0; d += 0.4)
  10. {
  11. Console.WriteLine("The cosine of {0} = {1}", d, Math.Cos(d));
  12. Console.WriteLine("Calculated cosine of {0} = {1}", d, cos(d));
  13. Console.WriteLine();
  14. }
  15. Console.ReadKey(); //keep console on screen until key press
  16. }
  17.  
  18. static double cos(double x) //calculate cosine
  19. {
  20. double p = x * x; //reduce amount of multiplications
  21. double q = p * p;
  22. return 1.0 - p / 2 + q / 24 - p * q / 720 + q * q / 40320 - p * q * q / 3628800;
  23. // add more terms for more accuracy
  24. // 2, 24,720 etc. are the factorials of 2,4,6 etc.
  25. }
  26. }
  27. }
Attached Images
File Type: bmp Cosine.bmp (24.0 KB, 5 views)
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
1
  #6
Oct 13th, 2009
A Taylor series isn't really a good way to compute this function unless you're looking for asymptotic accuracy around a particular point, rather than general accuracy along the whole thing. Also, it's a good idea to start by folding the number down to the interval [0,pi]. But suppose we did want to go with a Taylor series because that's all we knew of.
  1. static double cos(double x) //calculate cosine
  2. {
  3. // move x to value in [0, pi] with equal answer
  4. x = Math.Abs((x + Math.PI) % (2 * Math.PI) - Math.PI);
  5. const double tf = 1.0 / 24.0, vtz = -1.0 / 720.0, fzhtz = 1.0 / 40320.0, fukit = -1.0 / 3628800.0;
  6. double p = x * x;
  7. // use Horner's method instead, just because.
  8. return 1 + p * (-0.5 + p * (tf + p * (vtz + p * (fzhtz + p * fukit))));
  9. }
  10. }
  11. }

See pages 115 and 116 here for a comparison of a Taylor approximation with one that strives for a different metric of accuracy.
Last edited by Rashakil Fol; Oct 13th, 2009 at 2:35 pm.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

Tags
math

Message:


Thread Tools Search this Thread



Tag cloud for math
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC