在c++中求角的正弦和余弦值

finding sine and cosine values of an angle in c++

本文关键字:余弦值 c++      更新时间:2023-10-16

我是一个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;
}

我得到1.7949e-009作为cos(90)的结果。是否有办法得到0作为结果(在结果变量),而不是这种格式?sin180度也有同样的问题。我想要一个结果值为0的通解

既然你标注了c++而不是C,让我给你一些c++提示:

  1. 数学的标准标头是<cmath>而不是<math.h>
  2. 在c++中有更好的方式来声明常量#define
  3. 浮点数不是实数的精确表示(没有精确的计算表示可以存在),所以你总是以舍入错误结束。

得到结果的更习惯的方法是:

#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;
}

注意我是如何让std::显式的:c++ <cmath>比C有更多的数学函数重载。

:

  • std::位于
  • std::因为
  • std::设置精度

还请注意,尽管更精确的PI使result更准确,但结果总是有可能不完美,因此-当显示浮点值时-将精度设置为足以在对您的问题有意义的级别上补偿换流错误的级别。

实数的表示精度可以从std::numeric_limits<double>::digits10(来自<limits>报头)获得:切出2-3位总是好的。

此外,在进行减法或比较时,要考虑舍入误差:参见std::numeric_limits::epsilon参考文档中的示例:

#include <cmath>
#include <limits>
#include <iomanip>
#include <iostream>
#include <type_traits>
#include <algorithm>
template<class T>
typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
    almost_equal(T x, T y, int ulp)
{
    // the machine epsilon has to be scaled to the magnitude of the values used
    // and multiplied by the desired precision in ULPs (units in the last place)
    return std::abs(x-y) < std::numeric_limits<T>::epsilon() * std::abs(x+y) * ulp
    // unless the result is subnormal
           || std::abs(x-y) < std::numeric_limits<T>::min();
}
int main()
{
    double d1 = 0.2;
    double d2 = 1 / std::sqrt(5) / std::sqrt(5);
    if(d1 == d2)
            std::cout << "d1 == d2n";
    else
            std::cout << "d1 != d2n";
    if(almost_equal(d1, d2, 2))
            std::cout << "d1 almost equals d2n";
    else
            std::cout << "d1 does not almost equal d2n";
}

表明sqrt(5)的平方不是…5、即使你看起来是这样:

(扰流器:输出为

)
d1 != d2
d1 almost equals d2

); -)

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

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

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

范围缩小可以用fmod(x,360.0)精确地完成,并进一步使用各种三角函数标识。

这个答案提供了关于一般方法的信息和详细的sind(double degrees)。下面是结果值为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;
}

我试过M_PI, 3.141592653589793238L或acos(- 11)。所有这些PI的近似值在你的程序中不会产生0。但是,至少您可以使用std::setprecision和std::fixed(在iomanip中)来显示0。或者你可以用自定义的来四舍五入。

结果为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;
}
#include <stdio.h>      
#include <math.h>    
#define PI 3.14159265
int main (){
  double param, result;
  param = 30.0;
  result = sin (param*PI/180);
  printf ("The sine of %f degrees is %f.n", param, result );
  return 0;
}