gpt4 book ai didi

c++ - 在 C++ 中计算 tan(x) 为无穷大

转载 作者:行者123 更新时间:2023-11-27 23:14:09 26 4
gpt4 key购买 nike

#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);
}

我这里有几个问题:

  1. 当我输入90 度或 1.5708 弧度时,程序如何显示tan(x)无穷大?当我输入 90 度时,cos(x) 的输出为 0.0000013268 而不是 0。

  2. 我尝试将此命令放入我的 cosin 函数中,其中 如果 cos(x)<0.00001,将其设置为零,它适用于 90 度,但适用于其他值,如 2.3145 弧度, cos(x) 值为 0 而不是 -0.677013

感谢您的指导!

最佳答案

  1. 就像您在问题 #2 中提到的那样使用 epsilon 值。

  2. 在您的 if 语句中使用 cos(x) 的绝对值,例如 abs(cos(x))。 .

您还可以用 double 或 float 表示无穷大。检查此链接。 http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html

更重要的是,您可能想阅读这篇名为“每位计算机科学家应了解的浮点运算知识”的文章 http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

您会注意到,您的每一步浮点运算都会在计算中累积错误。

关于c++ - 在 C++ 中计算 tan(x) 为无穷大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17814507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com