Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c+中求角度的正弦和余弦值+; 我是C++新手,编写了一个小程序来求一个角的正弦和余弦值。我的示例代码如下: #include <math.h> #include <iostream> #include <iomanip> using namespace std; #define PI 3.14159265 int main () { double rate, result; rate = 90.0; result = cos (rate*PI/180); cout<<"The cosine of " << rate << " degrees is " << result <<endl; return 0; } #包括 #包括 #包括 使用名称空间std; #定义PI 3.14159265 int main() { 倍率,结果; 比率=90.0; 结果=cos(比率*PI/180); 不能包含 #包括 #定义PI 3.14159265 int main(){ 双参数,结果; 参数=30.0; 结果=sin(参数*PI/180); printf(“%f度的正弦为%f.\n”,参数,结果); 返回0; }_C++_Math_Trigonometry - Fatal编程技术网

在c+中求角度的正弦和余弦值+; 我是C++新手,编写了一个小程序来求一个角的正弦和余弦值。我的示例代码如下: #include <math.h> #include <iostream> #include <iomanip> using namespace std; #define PI 3.14159265 int main () { double rate, result; rate = 90.0; result = cos (rate*PI/180); cout<<"The cosine of " << rate << " degrees is " << result <<endl; return 0; } #包括 #包括 #包括 使用名称空间std; #定义PI 3.14159265 int main() { 倍率,结果; 比率=90.0; 结果=cos(比率*PI/180); 不能包含 #包括 #定义PI 3.14159265 int main(){ 双参数,结果; 参数=30.0; 结果=sin(参数*PI/180); printf(“%f度的正弦为%f.\n”,参数,结果); 返回0; }

在c+中求角度的正弦和余弦值+; 我是C++新手,编写了一个小程序来求一个角的正弦和余弦值。我的示例代码如下: #include <math.h> #include <iostream> #include <iomanip> using namespace std; #define PI 3.14159265 int main () { double rate, result; rate = 90.0; result = cos (rate*PI/180); cout<<"The cosine of " << rate << " degrees is " << result <<endl; return 0; } #包括 #包括 #包括 使用名称空间std; #定义PI 3.14159265 int main() { 倍率,结果; 比率=90.0; 结果=cos(比率*PI/180); 不能包含 #包括 #定义PI 3.14159265 int main(){ 双参数,结果; 参数=30.0; 结果=sin(参数*PI/180); printf(“%f度的正弦为%f.\n”,参数,结果); 返回0; },c++,math,trigonometry,C++,Math,Trigonometry,结果为1.7949e-009,这是科学的方法,您可以使用固定的方法,甚至指定点的精度 事实上,1.7949e-009约为0.0000000017949 用户krzaq指定输出格式为固定方式,并将精度设置为2,它将打印: the cosine of 90.0 degree is 0.00 此外,你的PI不够准确 要获得高精度,您需要做的唯一额外的事情就是下载glm。glm是一个出色的数学聚会,它在OpenGL数学函数中工作得非常出色。 以下是使用glm的代码: #include <iost

结果为1.7949e-009,这是科学的方法,您可以使用固定的方法,甚至指定点的精度

事实上,1.7949e-009约为0.0000000017949

用户krzaq指定输出格式为固定方式,并将精度设置为2,它将打印:

the cosine of 90.0 degree is 0.00
此外,你的PI不够准确

要获得高精度,您需要做的唯一额外的事情就是下载glm。glm是一个出色的数学聚会,它在OpenGL数学函数中工作得非常出色。 以下是使用glm的代码:

#include <iostream>
#include <glm.hpp>

int main()
{
    double Angle, Result;
    Angle  = 90.0;
    Result = glm::cos(glm::radians(Angle));
    std::cout << "The cosine of " << Angle << " degrees is " << std::fixed << Result << std::endl;
    return 0;
}
#包括
#包括
int main()
{
双角度,结果;
角度=90.0;
结果=glm::cos(glm::弧度(角度));

std::cout我尝试了M_PI,3.141592653589793238L或acos(-1l)。所有这些PI的近似值在您的程序中都不是精确的0。 但是,至少可以使用std::setprecision和std::fixed(在iomanip中)来显示0。
或者你可以使用自定义ε来循环结果。

因为你标记了后C++而不是C,让我给你一些C++提示:

  • 数学的标准标题是
    ,而不是
  • C++中有更好的方法来声明“代码>”定义常数
  • 浮点数不是实数的精确表示法(对于实数,不存在计算精确表示法),因此最终总是会出现舍入错误
  • 更惯用的方法是:

    #include <cmath>
    #include <iostream>
    #include <iomanip>
    
    int main ()
    {
        const auto PI = std::acos(-1); //let the  computer to find out what PI is
    
        double rate{}, result{}; //don't let uninitialized values
        rate = 90.0;
        result = std::cos (rate*PI/180);
        std::cout<<"The cosine of " << // set outoput precison for floating point
             std::setprecision(4) << rate << " degrees is " << 
             std::setprecision(4) << result <<endl;
        return 0;
    }
    
    这说明了sqrt(5)的平方不是…5,即使你看起来是这样的:

    (扰流器:输出是

    d1 != d2
    d1 almost equals d2
    
    );-)

    有没有办法得到0作为结果[对于余弦(90°)]

    步骤1,使用更精确的机器
    PI

    步骤2:不要转换为弧度,然后调用
    cos()
    ,而是缩小范围,然后转换为弧度,然后调用
    cos()

    可以使用
    fmod(x,360.0)
    进行范围缩小,还可以使用各种方法进一步缩小范围

    提供一般方法的信息和详细的
    sind(双度)
    。以下是结果值为0的情况的一般解决方案。讨论
    -0.0
    关注点

    // cos()  of 90.0 degrees is   6.1232339957367660e-17
    // cosd() of 90.0 degrees is   0.0000000000000000e+00
    
    #include <cmath>
    
    
    static double d2r(double d) {
      static const auto PI = std::acos(-1);
      return (d / 180.0) * PI;
    }
    
    double cosd(double x /* degrees */) {
      if (!isfinite(x)) {
        return std::cos(x);
      }
      int quo;
      double x90 = std::remquo(std::fabs(x), 90.0, &quo);
      double xr = d2r(x90);
      switch (quo % 4) {
        case 0:
          return std::cos(xr);
        case 1:
          // Use + 0.0 to avoid -0.0
          return std::sin(-xr + 0.0);
        case 2:
          return -std::cos(xr);
        case 3:
          return std::sin(xr + 0.0);
      }
      return 0.0;
    }
    
    //90.0度的cos()为6.1232339957367660e-17
    //90.0度的cosd()为0.0000000000000000 E+00
    #包括
    静态双d2r(双d){
    静态常数自动PI=std::acos(-1);
    回报率(d/180.0)*PI;
    }
    双余弦(双x/*度*/){
    如果(!isfinite(x)){
    返回std::cos(x);
    }
    现状;
    双x90=std::remquo(std::fabs(x),90.0,&quo);
    双xr=d2r(x90);
    交换机(quo%4){
    案例0:
    返回std::cos(xr);
    案例1:
    //使用+0.0避免使用-0.0
    返回标准::sin(-xr+0.0);
    案例2:
    返回-std::cos(xr);
    案例3:
    返回标准::sin(xr+0.0);
    }
    返回0.0;
    }
    
    Ditch
    #定义PI
    ,使用数学标题中的M#u PI。@n.M.这不是一个标准化的定义我会说你的PI不够精确(它的精度与你得到的结果大致相同)因此,试着给出它,比如说15位数的精度,然后再试一次,顺便说一下,在编写C++代码时更喜欢使用 CyMasa[/Cord]而不是< Case>数学> .H/COD>。它是更习惯的。@ KrZaq是由POSIX标准化的。不符合的系统可能会在地狱中被烧毁。但是它实际上不存储0结果变量。对吗?@upta
    printf
    ostream
    可能会使用不同的精度进行打印,但这只是处理一个症状。复制和粘贴自:有没有办法将精确值存储在结果变量中以备将来使用?@Francishoo结果值仍然是1.7949e-009,它没有被关键字fixed和setprecision(2)更改.关键字fixed和setprecision(2)只需限制输出格式。是的,我正在告诉你。但是我必须做些什么来存储精确的值以供使用?@Francishoo你的意思是你想得到更精确的结果值?你可以使用名为glm的第三方。它具有将角度转换为弧度的功能。然后结果将是6.1232339957367660e-017,大约是0.0000000000000000 6123233995736766。我将在我的答案中附加代码。不,我的意思是我将实际将0存储到结果中,而不是1.7949e-009。可能吗?@FrancisHoo
    d1 != d2
    d1 almost equals d2
    
    // cos()  of 90.0 degrees is   6.1232339957367660e-17
    // cosd() of 90.0 degrees is   0.0000000000000000e+00
    
    #include <cmath>
    
    
    static double d2r(double d) {
      static const auto PI = std::acos(-1);
      return (d / 180.0) * PI;
    }
    
    double cosd(double x /* degrees */) {
      if (!isfinite(x)) {
        return std::cos(x);
      }
      int quo;
      double x90 = std::remquo(std::fabs(x), 90.0, &quo);
      double xr = d2r(x90);
      switch (quo % 4) {
        case 0:
          return std::cos(xr);
        case 1:
          // Use + 0.0 to avoid -0.0
          return std::sin(-xr + 0.0);
        case 2:
          return -std::cos(xr);
        case 3:
          return std::sin(xr + 0.0);
      }
      return 0.0;
    }