一般語言裡都是直接調用函數,這些底層的函數是怎麼實現的?請提供代碼,謝謝。
好好學習數學,就可以實現這個代碼!泰勒公式---有趣!
$sin(x)= x-\dfrac{x^3}3!+ \dfrac{x^5}5!-\dfrac{x^7}7!·······$
$sin(x)= 1-\dfrac{x^2}2!+ \dfrac{x^4}4!-\dfrac{x^6}6!·······$
做題的時候就是在復習過去的知識--好醉,好玩!
代碼如下過去做的sin和改的cos:
#include
using namespace std;
#define pi 3.1415925
#include
int main()
{
double a=1, b=0, c=0, x,n,s;
cin >> x;
x = (x*pi) / 180;
s = x;
for (n = 3;; n += 2)
{
a = a*(n - 1)*n;
b = -b;
c = pow(x, n) / a;
s = s + b*c;
if (c <= pow(10, -6))
{
cout << "sin(x)=" << x << endl;
cout << "" << endl;
break;
}
}
cout << "原先"<<sin(x) << endl;
return 0;
}
改的cos:
#include
#include
#define pi 3.1415925
using namespace std;
int main()
{
double a = 1, b = 1, c = 0, x, n, s;
cin >> x;
x = (x*pi) / 180;
s = 1;
for (n = 2;; n += 2)
{
a = a*(n - 1)*n;
b = -b;
c = pow(x, n) / a;
s = s + b*c;
if (c <= pow(10, -6))
{
cout << "cos(x)=" << s << endl;
break;
}
}
cout << "原先" << cos(x) << endl;
return 0;
}
附圖: