C++ 计算tan(x)在C+中为无穷大+; #包括 #包括 #包括 使用名称空间std; 双余弦_值(双值); 双正弦值(双值); 双大_度(双值); 双倍大弧度(双倍值); 双x; 双重价值; 双学位; 双弧度; 常数双PI=3.14159; 字符选择; 是的; int main() { cout>值; 库特
使用ε值,就像你在问题2中提到的那样C++ 计算tan(x)在C+中为无穷大+; #包括 #包括 #包括 使用名称空间std; 双余弦_值(双值); 双正弦值(双值); 双大_度(双值); 双倍大弧度(双倍值); 双x; 双重价值; 双学位; 双弧度; 常数双PI=3.14159; 字符选择; 是的; int main() { cout>值; 库特,c++,C++,使用ε值,就像你在问题2中提到的那样 在if语句中使用绝对值cos(x),如abs(cos(x)) 你也可以用double或float来表示无穷大。检查这个链接。 更重要的是,您可能想阅读这篇文章,题为“每个计算机科学家都应该知道的浮点运算” 您会注意到浮点运算的每一步都会在计算中累积错误 使用ε值,就像你在问题2中提到的那样 在if语句中使用绝对值cos(x),如abs(cos(x)) 你也可以用double或float来表示无穷大。检查这个链接。 更重要的是,您可能想阅读这篇文章,题为
你会注意到浮点运算的每一步都会在计算中积累错误。你在哪里设置
度数
和弧度
?为什么不保存大度数
和大弧度
的返回值?@Dan455对不起,我不明白你的意思…我对这个数量感到惊讶代码重复和普遍的奇怪。很难理解程序的目标。所以你会问为什么输出只精确到小数点后6位?主要是因为你的PI
值只指定到小数点后6位。(还要注意,使用典型的64位双精度,无论您做什么,都不能希望小数位数超过17位)。你在哪里设置度数
和弧度
?为什么不保存大度数
和大弧度
的返回值?@Dan455抱歉,我不明白你的意思…我对其中大量的代码重复和普遍的奇怪感到惊讶。很难理解该程序的目标。因此,你是询问为什么输出仅精确到小数点后6位?主要是因为PI
的值仅指定为小数点后6位。(还要注意,使用典型的64位double
,无论您做什么,您都不能希望超过约17位)。您的意思是“如果(abs(余弦)<0.00001”)'要在我的余弦值函数中实现吗?你是说'如果(abs(余弦)<0.00001)'要在我的余弦值函数中实现吗?
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
double cosin_value( double value );
double sin_value( double value );
double big_degree( double value );
double big_radian( double value );
double x;
double value;
double degree;
double radian;
const double PI = 3.14159;
char choice;
char yes;
int main()
{
cout << "Please enter an angle value => ";
cin >> value;
cout << "Is the angle in Degree or Radian?" << endl;
cout << "\t" << "Type D if it is in Degree" << endl;
cout << "\t" << "Type R if it is in Radian" << endl;
cout << "Your response => ";
cin >> choice; //degree or radian?
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
if (choice == 'D' || choice == 'd')
{
big_degree (value);
cout << " " << "sin(x) = " << "\t" << sin_value(degree) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(degree) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(degree) / cosin_value(degree) << endl;
}
else if (choice == 'R' || choice == 'r')
{
big_radian (value);
cout << " " << "sin(x) = " << "\t" << sin_value(radian) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(radian) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(radian) / cosin_value(radian) << endl;
}
return 0;
}
// Sine,cosine functions
// angle -360<value<360
double sin_value( double value )
{
int count=1;
double sine, num, dem, sign, term;
sine = 0;
sign = 1;
num = value;
dem = count;
while ( count <= 20 )
{
term = ( num / dem );
sine = sine + term * sign;
num = num * value * value;
count = count + 2;
dem = dem * count * ( count - 1 );
sign = -sign;
}
return (sine);
}
double cosin_value( double value )
{
int count = 0;
double cosine, num, dem, sign, term;
cosine = 0;
sign = 1;
num = 1;
dem = 1;
while ( count <= 20 )
{
term = ( num / dem );
cosine = cosine + term * sign;
num = num * value * value;
count = count + 2;
dem = dem * count * ( count - 1 );
sign = -sign;
}
return (cosine);
}
double big_degree( double value )
{
int result;
const int angle = 360;
if (value >= 360 || value <= -360)
{
result = value / angle;
degree = ( value - ( result * angle ) ) * PI / 180;
}
else
{
degree = ( value * PI ) / 180;
}
return (degree);
}
double big_radian( double value )
{
int result;
if (value >= 2 * PI || value <= -2 * PI)
{
result = value / ( 2 * PI );
radian = ( value - ( result* 2 * PI ) );
}
else
{
radian = value;
}
return (radian);
}