如何解决 C++ 中浮点运算的舍入问题?
我遇到了一些浮点运算不准确的问题。我正在尝试根据加权公式计算分数,其中每个输入变量的权重约为下一个重要变量的 20 倍。然而,输入是实数,所以我最终使用双精度来存储结果。下面的代码存在丢失E1和E2之间差异的问题。
这段代码对性能敏感,所以我需要找到这个问题的有效答案。我想将我的输入乘以一百,然后使用 int (因为我认为这足够精确),但我怀疑这是最好的解决方案,因此出现了问题。
#include <iostream>
int main()
{
double score1, score2;
float a = 2.75 ;
float b = 5.25 ;
float c = 5.25 ;
float d = 2.75 ;
float E1 = 3 ;
float E2 = 6 ;
score1 = 20 * b - 1 * a + 0.05 * d /* - 0.0025 * c*/ + 0.0001 * E1 ;
score2 = 20 * b - 1 * a + 0.05 * d /* - 0.0025 * c*/ + 0.0001 * E2 ;
std::cout << score1 << std::endl;
std::cout << score2 << std::endl;
std::cin.get();
return 0;
}
//ouputs:
//102.388
//102.388
Im running into some issues with floating point arithmetic not being accurate. I'm trying to calculate a score based on a weighted formula where every input variable weighs about as much as 20 times the next significant one. The inputs however are real numbers, so I ended up using a double to store the result. The code below has the problem of losing the difference between E1 and E2.
This code is performance sensitive, so I need to find an efficient answer to this problem. I thought of multiplying my inputs by a hundred and then using an int (since that would be precise enough I think), but I doubt that is the best solution, hence the question.
#include <iostream>
int main()
{
double score1, score2;
float a = 2.75 ;
float b = 5.25 ;
float c = 5.25 ;
float d = 2.75 ;
float E1 = 3 ;
float E2 = 6 ;
score1 = 20 * b - 1 * a + 0.05 * d /* - 0.0025 * c*/ + 0.0001 * E1 ;
score2 = 20 * b - 1 * a + 0.05 * d /* - 0.0025 * c*/ + 0.0001 * E2 ;
std::cout << score1 << std::endl;
std::cout << score2 << std::endl;
std::cin.get();
return 0;
}
//ouputs:
//102.388
//102.388
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
cout << setprecision(number_of_digits) << score1 << endl;
鉴于您显示的值,我会说它是。
Given the values you've shown, I would say it is.
http://ideone.com/qqTB3 告诉你差异并没有丢失,而是实际上和你一样大' dexpect(最高浮点精度,对于 double 来说是 15 位小数)。
http://ideone.com/qqTB3 shows you that the difference is not lost, but actually as big as you'd expect (up to floating point accuracy, which is 15 decimal digits for double).
让我们看看这段代码中发生了什么:
如果我们在脑海中执行此操作:
这些值的精度应该保持不变:
但是当你打印出来时,双精度数的默认精度是小数点后 3 位。
因此设置精度:
Lets see what is happening in this code:
If we do this in our heads:
The precision should hold for those values:
But when you print out the default precision for doubles is 3 decimal places.
So set the precision: