2

在以下来源中,为什么DEG_TO_RAD_B常数计算为0

/// @file Main.cpp

// Includes

#include <cmath>
#include <iostream>


// Constant definitions

template <typename T>
const T PI
    = std::acos(-T(1));

const float DEG_TO_RAD_A                // good - evaluates to 0.0174533
    = std::acos(-float(1)) / 180.0f;

const float DEG_TO_RAD_B                //  bad - evaluates to 0
    = PI<float> / 180.0f;


/// Process entry point

int main()
{
    float deg_to_rad = PI<float> / 180.0f;

    std::cout << "PI:           " << PI<float>    << std::endl; // good - 3.14159
    std::cout << "deg_to_rad:   " << deg_to_rad   << std::endl; // good - 0.0174533
    std::cout << "DEG_TO_RAD_A: " << DEG_TO_RAD_A << std::endl; // good - 0.0174533
    std::cout << "DEG_TO_RAD_B: " << DEG_TO_RAD_B << std::endl; //  bad - 0

    return 0;
}

使用 Visual Studio Community 2019 版本 16.4.5 输出:

PI:           3.14159
deg_to_rad:   0.0174533
DEG_TO_RAD_A: 0.0174533
DEG_TO_RAD_B: 0

_MSC_VER : 1924
_MSC_FULL_VER : 192428316
_MSC_BUILD : 0
_MSVC_LANG : C++17

没有收到任何编译器警告。无论 x86/x64/debug/release 配置设置如何,输出相同。

4

0 回答 0