加倍会损失精度吗? [英] Loss of precision on adding doubles?

查看:80
本文介绍了加倍会损失精度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

民间!我遇到了一个小问题:我正在使用三个double值进行简单加法。结果的精度小于使用的值。

folks! I've encountered a little problem: I'm doing a simple addition with three double values. The result has a smaller precision than the used values.

double minutes = 3;
minutes = minutes / (24.0*60.0);  // contains 0.00208333
double hours = 3;
hours = hours / 24.0; // contains 0.125
double days = 3; // contains 3 

double age = days + hours + minutes; // result is 3.12708 

我发现无法避免这种行为。

I found no way to avoid this behaviour.

推荐答案

计算似乎没有问题,就像您帖子上的评论所说的那样。

Nothing seems to be wrong with the calculation as what the comments on your post said.

如果您想获得更高的精度,请考虑查找 setprecision()

If you'd like to see more precision consider looking up setprecision()

这篇关于加倍会损失精度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆