944,195 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2291
  • C# RSS
Oct 10th, 2009
0

find value of cos without using math library function

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ayeshawzd is offline Offline
10 posts
since Oct 2009
Oct 10th, 2009
0
Re: find value of cos without using math library function
Did you search the web? Or have you absolutely no clue as how to tackle your problem?
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 10th, 2009
1
Re: find value of cos without using math library function
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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 11th, 2009
0
Re: find value of cos without using math library function
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Oct 13th, 2009
0
Re: find value of cos without using math library function
Because cos(x) can be writen as an infinite series. See picture included. You could do something like this :

c# Syntax (Toggle Plain Text)
  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, 50 views)
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 13th, 2009
1
Re: find value of cos without using math library function
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.
C# Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Jan 11th, 2010
0
Re: find value of cos without using math library function
thanks u to alllll
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ayeshawzd is offline Offline
10 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Transparency / Opacity of Textbox
Next Thread in C# Forum Timeline: DB Connection IN c#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC