c++ - 在我下面编写的代码中,我想在代码中获取 tan 90 的值为 'INFINITY ' 。但机器得到的数字更高。我应该如何编辑代码?

标签 c++

我想制作一个可以显示所选度数的 sin、cos、tan、cot 值的表格。正如我所说,我无法接受 tan90 的正确答案。我不知道如何将其更改为“INFINITY”。我正在等待你的帮助! :)

#include<math.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

main()
{
    
    double table[10][4];
    int i=0,j=0;
    double val=0,PI=3.14159;
    val = PI / 180.0;

    printf("\tİstenen Tablo\n\t-------------\n");
    printf("\t ACI\t\t\t SIN\t\t\t COS\t\t\t TAN\t\t\t COT\n\t------\t\t\t------\t\t\t------\t\t\t--- 
 ---\t\t\t------");

    for(i=0;i<=90;i+=10)
    {
        for(j=0;j<5;j++)
        {
            switch (j)
            {
            case 0:table[i/10][j] = i; break;
            case 1:table[i/10][j] = sin(i*val);break;
            case 2:table[i/10][j] = cos(i*val);break;
            case 3:table[i/10][j] = tan(i*val);break;
            case 4:table[i/10][j] = 1/tan(i*val);break;
            }
        }
            printf("\n\t %lf  \t\t %lf\t\t %lf\t\t %lf\t\t %lf\n",table[i/10][0],table[i/10] 
 [1],table[i/10][2],table[i/10][3],table[i/10][4]); 
    }
   return 0;
}

最佳答案

您得到不正确值的原因是 π 不是 3.14159,这只是其实际值的近似值。事实上,任何非无限系列的十进制数字都将是近似值,但是数字越多,i * val 就越接近您的正确值< em>应该传递给你的三角函数调用。

更好的值是 math.h 中的 M_PI,它的精度比您当前的精度高得多。

由于 double 类型的精度有限,您仍然可能无法获得预期值。如果发生这种情况,您将需要在以浮点计算之前调整所得到的结果。

例如,可以使用以下函数来获取更可接受的值,在象限边界上强制使用非常具体的值,以避免这些结果中出现不精确的最小可能性:

double mySin(int degrees) {
    int mod360 = degrees % 360;

    if (mod360 ==   0) return  0.0;
    if (mod360 ==  90) return  1.0;
    if (mod360 == 180) return  0.0;
    if (mod360 == 270) return -1.0;

    return sin(degrees % 360 * M_PI / 180.0);
}

double myCos(int degrees) {
    return mySin(degrees + 90);
}

double myTan(int degrees) {
    int mod360 = degrees % 360;

    if (mod360 ==   0) return  0.0;
    if (mod360 ==  90) return  1.0 / 0.0;
    if (mod360 == 180) return  0.0;
    if (mod360 == 270) return -1.0 / 0.0;

    return tan(mod360 * M_PI / 180.0);
}

double myCot(int degrees) {
    // Now that tan() works properly for the quadrant
    // boundaries, just use normal formula.

    return 1.0 / myTan(degrees);
}

这些是从头开始构建的,使用度数输入而不是弧度,因为您在进行任何 float 学之前捕获无限值情况(当您处理时,这本质上是不精确的)超越),您可以为这些情况给出正确的结果。


一个完整的程序,集成了这些功能并删除了不需要的东西,如下所示。当您可以立即打印这些值时,将所有这些值存储在数组中然后打印出该数组(没有其他用途)是没有意义的:

#include <cmath>
#include <cstdio>

using namespace std;

double mySin(int degrees) {
    int mod360 = degrees % 360;

    if (mod360 ==   0) return  0.0;
    if (mod360 ==  90) return  1.0;
    if (mod360 == 180) return  0.0;
    if (mod360 == 270) return -1.0;

    return sin(mod360 * M_PI / 180.0);
}

double myCos(int degrees) {
    return mySin(degrees + 90);
}

double myTan(int degrees) {
    int mod360 = degrees % 360;

    if (mod360 ==   0) return  0.0;
    if (mod360 ==  90) return  1.0 / 0.0;
    if (mod360 == 180) return  0.0;
    if (mod360 == 270) return -1.0 / 0.0;

    return tan(mod360 * M_PI / 180.0);
}

double myCot(int degrees) {
    // Now that tan() works properly for the quadrant
    // boundaries, just use normal formula.

    return 1.0 / myTan(degrees);
}

int main()
{
    printf("İstenen Tablo\n");
    printf("-------------\n");
    printf("\t    ACI     \t    SIN    \t    COS    \t    TAN    \t    COT\n");
    printf("\t------------\t-----------\t-----------\t-----------\t-----------\n");

    for (int i = 0; i < 360; i += 10) {
        printf("\t%12d\t%9.9lf\t%9.9lf\t%9.9lf\t%9.9lf\n",
            i, mySin(i), myCos(i), myTan(i), myCot(i));
    }
   return 0;
}

其输出是:

İstenen Tablo
-------------
            ACI             SIN             COS             TAN             COT
        ------------    -----------     -----------     -----------     -----------
                   0    0.000000000     1.000000000     0.000000000           inf
                  10    0.173648178     0.984807753     0.176326981     5.671281820
                  20    0.342020143     0.939692621     0.363970234     2.747477419
                  30    0.500000000     0.866025404     0.577350269     1.732050808
                  40    0.642787610     0.766044443     0.839099631     1.191753593
                  50    0.766044443     0.642787610     1.191753593     0.839099631
                  60    0.866025404     0.500000000     1.732050808     0.577350269
                  70    0.939692621     0.342020143     2.747477419     0.363970234
                  80    0.984807753     0.173648178     5.671281820     0.176326981
                  90    1.000000000     0.000000000           inf       0.000000000
                 100    0.984807753     -0.173648178    -5.671281820    -0.176326981
                 110    0.939692621     -0.342020143    -2.747477419    -0.363970234
                 120    0.866025404     -0.500000000    -1.732050808    -0.577350269
                 130    0.766044443     -0.642787610    -1.191753593    -0.839099631
                 140    0.642787610     -0.766044443    -0.839099631    -1.191753593
                 150    0.500000000     -0.866025404    -0.577350269    -1.732050808
                 160    0.342020143     -0.939692621    -0.363970234    -2.747477419
                 170    0.173648178     -0.984807753    -0.176326981    -5.671281820
                 180    0.000000000     -1.000000000    0.000000000           inf
                 190    -0.173648178    -0.984807753    0.176326981     5.671281820
                 200    -0.342020143    -0.939692621    0.363970234     2.747477419
                 210    -0.500000000    -0.866025404    0.577350269     1.732050808
                 220    -0.642787610    -0.766044443    0.839099631     1.191753593
                 230    -0.766044443    -0.642787610    1.191753593     0.839099631
                 240    -0.866025404    -0.500000000    1.732050808     0.577350269
                 250    -0.939692621    -0.342020143    2.747477419     0.363970234
                 260    -0.984807753    -0.173648178    5.671281820     0.176326981
                 270    -1.000000000    0.000000000          -inf       -0.000000000
                 280    -0.984807753    0.173648178     -5.671281820    -0.176326981
                 290    -0.939692621    0.342020143     -2.747477419    -0.363970234
                 300    -0.866025404    0.500000000     -1.732050808    -0.577350269
                 310    -0.766044443    0.642787610     -1.191753593    -0.839099631
                 320    -0.642787610    0.766044443     -0.839099631    -1.191753593
                 330    -0.500000000    0.866025404     -0.577350269    -1.732050808
                 340    -0.342020143    0.939692621     -0.363970234    -2.747477419
                 350    -0.173648178    0.984807753     -0.176326981    -5.671281820

请注意无穷大和强制零上的符号之间的差异。虽然 0/10/-1 都可以被视为零(尽管 IEEE754 允许负零),但 1 的值/0-1/0分别被赋予+inf-inf

我的数学高手可能不同意,但我认为我是对的。

关于c++ - 在我下面编写的代码中,我想在代码中获取 tan 90 的值为 'INFINITY ' 。但机器得到的数字更高。我应该如何编辑代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67040009/

相关文章:

c++ - ROS Linking errors using boost::filesystem library 在linux下使用C++

c++ - 无法和谐配置C++、GDB、VSCode。收到错误消息 : `Debuggee TargetArchitecture not detected`

c++ - 改进WM_PAINT和WM_CTLCOLORSTATIC处理程序的代码

c++ - 下载 g++ 到 windows 是否保证您将成功编译 Unix 项目?

c++ - 静态链接 C++ 2010 失败

c++ - 在哪里可以找到所有数学函数的描述,比如 floorf 和其他函数?

c++ - 使用通用引用的可选引用技巧?

c++ - 复合赋值运算符的类型/转换? (例如 *=(星号等于))

c++ - 将十进制数转换为二进制数的意外结果

c++ - 如何找到数字的反对数(以 2 为底)?