C++ 因为给出了错误的值 #包括 #包括 #包括“boost/math/constants/constants.hpp” 常量双pi=boost::math::constants::pi(); int main(){ 双x=2; 双y=1; 双角度=90;//以度为单位 双rad{angle*pi/180};//转换为rads std::cout

C++ 因为给出了错误的值 #包括 #包括 #包括“boost/math/constants/constants.hpp” 常量双pi=boost::math::constants::pi(); int main(){ 双x=2; 双y=1; 双角度=90;//以度为单位 双rad{angle*pi/180};//转换为rads std::cout,c++,trigonometry,C++,Trigonometry,cos(90)(度)近似为零,如0.0中所示。担心得到一个e-17与e-9的数字没有意义,它们基本上都是零 结果的实际接近程度取决于它们如何表示pi,90.0,180.0,以及math.h中cos函数的实现 它应该接近-3.205103E-9,但我最终得到了6.12303e-017 我不知道你为什么认为它“应该是-3.205103E-9”,因为cos(90º)是零,-0.00000000 32051033远远超出了你计算的误差范围 您得到的值,0.0000000000000000 612303为

cos(90)
(度)近似为零,如
0.0
中所示。担心得到一个
e-17
e-9
的数字没有意义,它们基本上都是零

结果的实际接近程度取决于它们如何表示
pi
90.0
180.0
,以及
math.h
cos
函数的实现

它应该接近-3.205103E-9,但我最终得到了6.12303e-017

我不知道你为什么认为它“应该是-3.205103E-9”,因为
cos(90º)
是零,
-0.00000000 32051033
远远超出了你计算的误差范围


您得到的值,
0.0000000000000000 612303
为零,因此在计算误差范围内是正确的。

您的代码中没有错误

在cos函数(cos)中,返回值是实数

#include <iostream>
#include <math.h>
#include "boost/math/constants/constants.hpp"
const double pi = boost::math::constants::pi<double>();

int main() {
    double x = 2;
    double y = 1;
    double angle = 90; //in degrees
    double rad { angle * pi / 180 }; //converting to rads
    std::cout << "rad: " << rad << std::endl; //1.5708 - OK
    double c = cos( rad );
    std::cout << "cos(rad): " << c << std::endl; //6.12303e-017 - Huh? Should be ~ -3.2051033e-9
    double s = sin( rad );
    std::cout << "sin(rad): " << s << std::endl; //1 - OK
    x = x * c - y * s;
    y = x * s + y * c;
    std::cout << "(" << x << "," << y << ")" << std::endl;

    return 0;
}

你也可以用逆逆逆逆逆逆逆逆逆逆

确实是0。为什么您认为
-3.2e-9
6.1e-17
更正确?当您认为标准库数学函数错误时,您就错了。双精度数学最多生成15个有效数字。期望-3E-9作为理想值(0.0)的近似值没有任何意义,距离不够近。6E-17很好。也许你已经习惯了看到单精度的结果。你是从哪里获得
-3.2051033e-9
的结果的?@hometoos,当我手动进行计算时,从操作系统的计算器中获得的。坦白地说,
-3.2e-9
的舍入误差对于使用(64位)
double
s。这在使用(32位)
float
s的计算中是有意义的。90000度怎么样?单和双给出了荒谬的错误。cos(90000°)不是OP的计算(而且
-3.2e-9
对它来说仍然是一个惊人的大舍入误差)。
(cos -1) returns 0.540302
(cos 0.0) returns 1.0
(cos (* pi 0.5)) returns 6.12303e-017 which is practically equal to **0**