| | |
find value of cos without using math library function
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
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.
I was then going to suggest that ddanbe is your man for this question, but I see he is already on top of it.
0
#5 Oct 13th, 2009
Because cos(x) can be writen as an infinite series. See picture included. You could do something like this :
c# Syntax (Toggle Plain Text)
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { for (double d = 0; d < 3.0; d += 0.4) { Console.WriteLine("The cosine of {0} = {1}", d, Math.Cos(d)); Console.WriteLine("Calculated cosine of {0} = {1}", d, cos(d)); Console.WriteLine(); } Console.ReadKey(); //keep console on screen until key press } static double cos(double x) //calculate cosine { double p = x * x; //reduce amount of multiplications double q = p * p; return 1.0 - p / 2 + q / 24 - p * q / 720 + q * q / 40320 - p * q * q / 3628800; // add more terms for more accuracy // 2, 24,720 etc. are the factorials of 2,4,6 etc. } } }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
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.
See pages 115 and 116 here for a comparison of a Taylor approximation with one that strives for a different metric of accuracy.
C# Syntax (Toggle Plain Text)
static double cos(double x) //calculate cosine { // move x to value in [0, pi] with equal answer x = Math.Abs((x + Math.PI) % (2 * Math.PI) - Math.PI); const double tf = 1.0 / 24.0, vtz = -1.0 / 720.0, fzhtz = 1.0 / 40320.0, fukit = -1.0 / 3628800.0; double p = x * x; // use Horner's method instead, just because. return 1 + p * (-0.5 + p * (tf + p * (vtz + p * (fzhtz + p * fukit)))); } } }
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.
![]() |
Similar Threads
- Library function (C++)
- i have some questions about the math Library (C)
- Tips to make your program more efficient (C++)
- its ny home work pla helpppppppppppp (C)
- Help me with "Math Library" Related coding =) (C++)
Other Threads in the C# Forum
- Previous Thread: Implementing FIX protocol in C#
- Next Thread: What will you recommend me to do or master?
Views: 699 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for math
algorithm angle array bitmap c# c++ conversion cuil decimal degrees development division drawing enum equation form function gcd gdi+ google grade iteration javascript mandelbrot math mathematics method news numbers operator picturebox plotting polynomial prime primenumbersinrange radians recursion recursive round search statistics technology time usb vb vbnet web wolframalpha







