// cache to reduce number of recursion
static Dictionary<int, int> _FactCache = new Dictionary<int, int>();
static int Fact(int input)
{
if (input < 0) throw new ArgumentOutOfRangeException("Fact for < 0 is no defined");
if (_FactCache.ContainsKey(input)) return _FactCache[input];
if (input <= 1) return 1;
int result = input * Fact(input - 1);
_FactCache.Add(input, result);
return result;
}
static double Sin(double x)
{
double result = x;
bool minus = true;
int power = 3;
while (true)
{
if (double.IsInfinity(Math.Pow(x, power))) break;
double currentResult = Math.Pow(x, power) / Fact(power);
if (double.IsInfinity(result - currentResult)) break;
if (double.IsInfinity(result + currentResult)) break;
if (minus)
{
result -= currentResult;
}
else
{
result += currentResult;
}
minus = !minus;
power++;
}
return result;
}
ของผมคับ คิดออกมาแล้วเลขมันเพี้ยนไปเยอะเลย แต่ว่า ถ้ายิ่งค่าองศา (เรเดียน) น้อยๆ มันจะใกล้กันครับ
PI/1
My: 5295936.90699457
Math: 1.22460635382238E-16
PI/2
My: 1.11493617480346
Math: 1
PI/3
My: 0.897003518476566
Math: 0.866025403784439
PI/4
My: 0.718309316964881
Math: 0.707106781186547
PI/5
My: 0.592733064504933
Math: 0.587785252292473
PI/6
My: 0.502504559480968
Math: 0.5
PI/7
My: 0.435282086701046
Math: 0.433883739117558
PI/8
My: 0.383523785669715
Math: 0.38268343236509
PI/9
My: 0.342554898220715
Math: 0.342020143325669
PI/10
My: 0.309373199761157
Math: 0.309016994374947
PI/11
My: 0.281978858066394
Math: 0.28173255684143
PI/12
My: 0.258994728365534
Math: 0.258819045102521
PI/13
My: 0.239444311322423
Math: 0.239315664287558
PI/14
My: 0.222617279785313
Math: 0.222520933956314
PI/15
My: 0.207985263740032
Math: 0.207911690817759
PI/16
My: 0.195147468636724
Math: 0.195090322016128
PI/17
My: 0.183794576135041
Math: 0.18374951781657
PI/18
My: 0.173684180981053
Math: 0.17364817766693
PI/19
My: 0.164623702813137
Math: 0.164594590280734
