0
#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 度时,它给了我 0.0000013268 的输出,而不是 cos(x) 的 0。

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

我很感激你的指导!

4

1 回答 1

4
  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

更重要的是,您可能想阅读这篇名为“What Every Computer Scientist Should Know About Floating-Point Arithmetic”的文章 http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

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

于 2013-07-23T15:39:54.553 回答