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
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
Did you search the web? Or have you absolutely no clue as how to tackle your problem?
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.;)
Because cos(x) can be writen as an infinite series. See picture included. You could do something like this :
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.
}
}
}
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.
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.
thanks u to alllll