计算如下公式,并输出结果:其中,r和s的值由键盘输入。(有sin x的近似值计算公式。)

计算:d=\begin{cases}\sqrt{sin^{2} r+ sin^{2} s} & \text{ if } r^{2}\leq s^{2} \\ \frac{1}{2}sin(rs) & \text{ if } r^{2}>s^{2} \end{cases}

sin x 公式:sin x = \frac{x}{1!}-\frac{x^{3}}{3!}+\frac{x^{5}}{5!}-\frac{x^{7}}{7!}+...=\sum_{n=1}^{\infty }(-1)^{n-1}\frac{x^{2n-1}}{(2n-1)!}计算精度为10^{-10}

sin x 函数调用设置,不用系统函数库给的,自己设置,自己调用

double tsin(double x) {
	double g = 0;
	double t = x;
	int n = 1;
	do {
		g += t;
		n++;
		t = -t * x * x / (2 * n - 1) / (2 * n - 2);
	} while (fabs(t) >= TINY_VALUE);
	return g;
}

。全部代码:

#include<iostream>
#include<cmath>
using namespace std;
const double TINY_VALUE = 1e-10;
double tsin(double x) {
	double g = 0;
	double t = x;
	int n = 1;
	do {
		g += t;
		n++;
		t = -t * x * x / (2 * n - 1) / (2 * n - 2);
	} while (fabs(t) >= TINY_VALUE);
	return g;
}
int main() {
	double k, r, s;
	cout << "r = ";
	cin >> r;
	cout << "s = ";
	cin >> s;
	if (r * r <= s * s)
		k = sqrt(tsin(r) * tsin(r) + tsin(s) * tsin(s));
	else
		k = tsin(r * s) / 2;
	cout << k << endl;
	return 0;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值