在c++中求角的正弦和余弦值
finding sine and cosine values of an angle in 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;
}
我得到1.7949e-009作为cos(90)的结果。是否有办法得到0作为结果(在结果变量),而不是这种格式?sin180度也有同样的问题。我想要一个结果值为0的通解
既然你标注了c++而不是C,让我给你一些c++提示:
- 数学的标准标头是
<cmath>
而不是<math.h>
- 在c++中有更好的方式来声明常量
#define
- 浮点数不是实数的精确表示(没有精确的计算表示可以存在),所以你总是以舍入错误结束。
得到结果的更习惯的方法是:
#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;
}
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为"adjacent"变量赋值时出现问题
- HEX值到wchar_t字符(UTF-8)的转换
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 数组索引的值没有增加
- std::向量与传递值的动态数组
- 使用指针从C++中的数组中获取最大值
- 将值指定给向量(2D)的向量中的某个位置
- (C++)分析树以计算返回错误值的简单算术表达式
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 使用一个考虑到std::map中键值的滚动或换行的键
- C++中的赋值发生,尽管右侧出现异常
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 使用递归的数组的最小值.这是怎么回事
- C++将浮点指针值舍入为小数位数
- 在矢量中使用值(特征)来计算OpenCV的余弦相似性
- 余弦没有计算出确切的值
- 在c++中求角的正弦和余弦值