在C++用泰勒系列寻找sinx值
In C++ finding sinx value with Taylor's Series
我正试图用c++写一段代码,用泰勒级数计算sinX值。
#include <iostream>
using namespace std;
// exp example
#include <cstdio> // printf
#include <cmath> // exp
double toRadians(double angdeg) //convert to radians to degree
{ //x is in radians
const double PI = 3.14159265358979323846;
return angdeg / 180.0 * PI;
}
double fact(double x) //factorial function
{ //Simply calculates factorial for denominator
if(x==0 || x==1)
return 1;
else
x * fact(x - 1);
}
double mySin(double x) //mySin function
{
double sum = 0.0;
for(int i = 0; i < 9; i++)
{
double top = pow(-1, i) * pow(x, 2 * i + 1); //calculation for nominator
double bottom = fact(2 * i + 1); //calculation for denominator
sum = sum + top / bottom; //1 - x^2/2! + x^4/4! - x^6/6!
}
return sum;
}
int main()
{
double param = 45, result;
result = mySin(toRadians(param)); //This is my sin value
cout << "Here is my homemade sin : " << result << endl;
result = sin(param); //This is library value
cout << "Here is the API sin : " << result << endl;
return 0;
}
所以我的程序工作没有任何错误。我的输出就是:
这是我自制的sin: nan
这是API sin:0.850904
我知道我犯了一个很大的逻辑错误,但是我找不出来。这是我使用c++的第二周。我对Java比较熟悉。我也写了同样的代码,而且运行得非常完美。答案是一致的。感谢您的时间和关注!
-
在
fact
中,您错过了返回:x*fact(x-1);
应该是return x*fact(x-1);
。如果打开警告,可以看到编译器发出抱怨。例如,在GCC中,调用g++ -Wall program.cpp
将得到Warning: control reaches end of non-void function
作为阶乘函数。 -
API
sin
也需要以弧度为单位的角度,因此将result=sin(param);
更改为result=sin(toRadians(param));
。一般来说,如果对API有疑问,请参阅文档,如这里。
你的代码似乎有一些逻辑错误。下面是我的更正:
#include <iostream>
using namespace std;
double radians(double degrees) // converts degrees to radians
{
double radians;
double const pi = 3.14159265358979323846;
radians = (pi/180)*degrees;
return radians;
}
double factorial(int x) //calculates the factorial
{
double fact = 1;
for(; x >= 1 ; x--)
{
fact = x * fact;
}
return fact;
}
double power(double x,double n) //calculates the power of x
{
double output = 1;
while(n>0)
{
output =( x*output);
n--;
}
return output;
}
float sin(double radians) //value of sine by Taylors series
{
double a,b,c;
float result = 0;
for(int y=0 ; y!=9 ; y++)
{
a= power(-1,y);
b= power(radians,(2*y)+1);
c= factorial((2*y)+1);
result = result+ (a*b)/c;
}
return result;
}
double n,output;
int main()
{
cout<<"enter the valuet";
cin>>n;
n = radians(n);
cout<< "nthe value in radians ist"<< n << "n";
output = sin(n);
cout<< "nsine of the given value ist"<< output;
return 0;
}
这个程序的目的是使用自定义函数而不是库,使其他人更容易学习。
这个程序中有四个用户定义的函数。前三个用户定义的函数radians(), factorial(), power(),显然是简单的函数,执行的操作就像它们的名字一样。
第四个函数'sin()'以弧度为输入,弧度由函数'radians()'给出。sin函数在函数的'for(int y= 0;y!=9;y++)'循环中使用泰勒级数逐项迭代,直到第九次迭代来计算输出。for()循环迭代一般的数学表达式:Term(n)=(-1)^n .(x^(2n+1))/(2n+1)!
sin(x)= x- x^3/3! + x^5/5! -x^7/7! + x^9/9!
=x-x^3/2*3 (1- x^2/4*5 + x^4/4*5*6*7 + x^6/4*5*6*7*8*9)
=x - x^3/2*3 {1- x^2/4*5(1- x^2/6*7 + x^4/6*7*8*9)}
=x - x^3/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}]
=x(1 - x^2/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}])
double sin_series_recursion(double x, int n){
static double r=1;
if(n>1){
r=1-((x*x*r)/(n*(n-1)));
return sin_series_recursion(x,n-2);
}else return r*x;
}
- QT在错误的班级中寻找空位
- 正在寻找C++不可变的hashset/hashmap
- 寻找地理和伤害意味着超载
- 寻找一种更好的方法来表示无符号字符数组
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量
- 寻找闰年的逻辑是如何运作的?
- C++:寻找"returning address of local variable..."的更正
- 在C++中寻找排列和组合
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 为表示一个或多个操作的C++函数的int参数寻找类型安全的替换
- 寻找解决这个动态编程问题的提示
- 寻找最小楼梯成本的动态规划问题的错误答案
- 具有多个继承共享一个资源的对象 - 寻找良好的设计模式
- Homebrew OS X OpenCV - 在另一台Mac上运行时寻找dylib
- 我只是在寻找模板,在我的书中找到了这段代码,这显示了隔离错误?
- 寻找C++中数组和cout交互的解决方案
- C++原理和练习练习-从输入值n中寻找素数
- 寻找更好的方法来编写这个程序 C++
- 如何使用OpenCV作为运动检测器(而不是寻找视频输出)
- 在C++用泰勒系列寻找sinx值