小编Bri*_*nck的帖子

将 cmath 包含在 math.h 上会导致错误的结果

编辑测试用例和编译器不一致。最近我做了一个更改,使用更现代的 < cmath > 而不是 < math.h >。虽然编译得很好,没有任何警告,但对一个文件的这一行更改导致我的测试用例失败。根据我的理解,cmath 只是将大部分函数/变量放在 std 命名空间中,并且我没有对该命名空间进行任何更改。

我会注意到我正在使用

  • 开方

  • abs (因为我在这里使用双精度,所以我假设编译器会自动使用 cmath/math.h 形式,而不是 stdlib)

  • 晒黑

排除可能受此单行更改影响的任何文件。我确实有一个已经使用 cmath 的不同文件,但不影响测试。

一个测试用例

#include <iostream>
#include <cmath>
int main() {

    double x = -0.546;
    double y = abs( x / sqrt(x*x + 1.0));
    double z = std::abs( x / sqrt(x*x + 1.0));

    std::cout << "Variable value is " << x << std::endl;
    std::cout << "Value of function using cmath " << z << std::endl;
    std::cout << "Value of function using …
Run Code Online (Sandbox Code Playgroud)

c++ std math.h cmath

1
推荐指数
1
解决办法
295
查看次数

标签 统计

c++ ×1

cmath ×1

math.h ×1

std ×1