cmath和math.h给出了不同的答案

Pet*_*ter 2 c++ floating-point

当我使用cmath的atan函数和浮点数的数学时,我似乎得到了不同的答案:

#include <cmath>  
#include <math.h>                                                                              

#include <iostream>                                                             
#include <iomanip>                                                              

int main() {                                                                
    std::cout << std::setprecision(20) << atan(-0.57468467f) << std::endl;   
    std::cout << std::setprecision(20) << std::atan(-0.57468467f) << std::endl;  

    // I get:
    // -0.52159727580733605823
    // -0.52159726619720458984
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?两个库是否以不同方式实现?

jwo*_*der 7

math.hatan需要双重并返回一个双,然而cmath的过载,使得float参数(如这里使用的)将被用作浮法和产生float结果.因此,输出的差异来自使用两种不同的浮点类型.为了让他们使用相同类型的,或者删除f这些数字的两端或先更改atanatanf.