当总和本身显示为180时,当我从两个角度的总和中提取180时,我会得到错误的答案
Why I get an incorrect answer when I substract 180 from the sum of the two angles when the sum itself shows as 180?
该程序获得4分(它们的坐标),形成一个凸形图,应该给我A2 角度A4-180度的角度。示例:
1 3
0 2
1 1
2 2
我得到ua2 = 90和ua4 = 90,但是当我做UA2 UA4-180时,我得到2.0568E-007,而不是0。但是当我只做UA2 UA4时,我会得到180。对不起,如果A提出了类似的问题,但我不知道如何搜索它。谁能告诉我为什么会发生以及如何修复它以显示正确的答案?代码:
#include <iostream>
#include <fstream>
#include <cmath>
#define PI 3.14159265
using namespace std;
ifstream f("date.in");
struct punct
{
double x,y;
}A1,A2,A3,A4;
int main()
{
double uA2,uA4;
f>>A1.x>>A1.y>>A2.x>>A2.y>>A3.x>>A3.y>>A4.x>>A4.y;
//calculate cos of both angles
uA2=((A1.x-A2.x)*(A3.x-A2.x)+(A1.y-A2.y)*(A3.y-A2.y))/
(sqrt((A1.x-A2.x)*(A1.x-A2.x)+(A1.y-A2.y)*(A1.y-A2.y))*sqrt((A3.x-A2.x)*(A3.x-A2.x)+(A3.y-A2.y)*(A3.y-A2.y)));
uA4=((A1.x-A4.x)*(A3.x-A4.x)+(A1.y-A4.y)*(A3.y-A4.y))/
(sqrt((A1.x-A4.x)*(A1.x-A4.x)+(A1.y-A4.y)*(A1.y-A4.y))*sqrt((A3.x-A4.x)*(A3.x-A4.x)+(A3.y-A4.y)*(A3.y-A4.y)));
//calculate angles
uA2=acos(uA2)*180.0/PI;
uA4=acos(uA4)*180.0/PI;
//the part that gives me an incorrect answer
cout<<uA2+uA4-180<<endl;
}
用于浮点计算,我们可以使用一些非常小的偏移值来实现边际错误,例如1E-6。
double diff = uA2 + uA4 - 180, threshold = 1e-6;
if (diff < threshold && diff > -threshold) cout << "0" << endl;
else cout << diff << endl;
// cout<<uA2+uA4-180<<endl;
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 在C++中使用重载提取运算符时出现问题
- 在C++继承层次结构时提取实现者
- 使用ffmpeg时如何提高图像导出/提取速度?
- 提取 try-catch 时出现运行时错误
- 将文本文件数据读入字符数组时提取运算符的歧义
- 使用 substr 提取文本文件时出现问题
- 如何在每次运行时将数字从循环中提取出来
- 从特征::等距3d提取旋转时出错
- 提取操作员在使用来自其他类名的指针时超载
- 当总和本身显示为180时,当我从两个角度的总和中提取180时,我会得到错误的答案
- 提取ndarray时增强nullptr
- 从 CSV C++中提取双精度值时出现意外结果
- 编译器在遇到提取或插入运算符时处理信息(字符串、操纵器等)的顺序是什么?
- 是否保证标准提取运算符>>在失败时不会更改参数?
- 读取文件后提取子向量时的C 错误
- 遍历 QHash 并提取重复值的键时遇到问题
- C++ 在将字符串提取到标量时处理溢出/下溢
- 尝试在 eclipse CDT 中提取函数时"Too many declarations in selection"错误消息
- 当IMG(ROI)未返回正确的值时,如何正确提取ROI