sin()函数的实现

计算如下公式,并输出结果:

 

其中r、s的值由键盘输入。sin x的近似值按如下公式计算,计算精度为10-10

 

程序说明:

  #include <math.h>和#include<cmath>的区别:

    #include <math.h>为C语言中的库函数,由于C++兼容C,所以仍可以使用此函数库

    在C++中推荐使用 #include <cmath>, 两者中的函数以及使用方法几乎完全相同

C++代码如下:

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 const double TINY_VALUE = 1e-10;  //计算精度
 5 
 6 double tsin(double x) {   //为了和标准库中的sin()函数区别,所以取名为tsin()函数
 7     double n = x,sum=0;
 8     int i = 1;
 9     do
10     {
11         sum += n;
12         i++;
13         n = -n * x*x / (2 * i - 1) / (2 * i - 2);
14 
15     } while (fabs(n)>=TINY_VALUE);
16     return sum;
17 }
18 int main() {
19     int r, s;
20     double k;
21     cin >> r >> s;
22     if (r*r <= s * s) 
23         k = sqrt(tsin(r)*tsin(r) + tsin(s)*tsin(s));
24     else 
25         k = tsin(r*s) /2 ;
26     cout << k;
27     return 0;
28 }

猜你喜欢

转载自www.cnblogs.com/pgzhang/p/9069623.html