执行及代码:
/* *Copyright (c)2014,烟台大学计算机与控制project学院 *All rights reserved. *文件名:d.cpp *作 者:张旺华 *完毕日期:2014年11月10日 *版 本 号:v1.0 * *问题描写叙述:求sin(π/2)和sin(56° )的值精度要求达到小数点后6位; *输入描写叙述:无 *程序输出:sin(π/2)和sin(56° )的值 */ #include <iostream> using namespace std; double mysin(double); double myabs(double); const double pi = 3.1415926; int main() { cout << "sin(π/2)的值为" << mysin(pi / 2) << endl; cout << "sin(56° )的值为" << mysin(pi * 2 * 56 / 360) << endl; return 0; } double mysin(double x) { double sum = x, fact = x, n; int i = 1, s = 1, m = 1; do { m = m * (i + 1) * (i + 2); fact=fact*x*x; n = fact / m; s = -s; sum = sum + s * n; i+=2; } while (myabs(n) > 1e-5); return sum; } double myabs(double c) { if (c < 0) c = -c; else c = c; return c; }
执行结果: