C++ 如何在C++中有效地将double转换为int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14014261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:53:34  来源:igfitidea点击:

How to effectively convert double to int in c++?

c++

提问by StudentX

Sorry for not being specific, I just thought the context isn't important.

抱歉没有具体说明,我只是认为上下文并不重要。

Anyway the question can be seen as an extension of my other question on Progressbars in win32should I put the whole code here or the link is enough ?

无论如何,这个问题可以看作是我在 win32 中的 Progressbars上的另一个问题的扩展,我应该把整个代码放在这里还是链接就足够了?

The problem in its simplest form can be described as :

最简单形式的问题可以描述为:

double d1 = x.xxxxxx;
double d2 = x.xxxxxx;
double d3 = x.xxxxxx;
double d4 = x.xxxxxx;
double d5 = x.xxxxxx;
...
...
double dn = x.xxxxxx;


int i1 = (int)d1;
int i2 = (int)d2;
int i3 = (int)d3;
int i4 = (int)d4;
int i5 = (int)d5;
...
...
int in = (int)dn;

int i = i1+i2+i3+i4+i5+...+in;
double d = d1+d2+d3+d4+d5+...+in;

now i needs to be not less then d - 0.5;

How to do that ?

怎么做 ?

EDIT : Code modified. EDIT 2 : The number of n can not be predicted, and it is possible that d1,d2,...,dn are less then 1, something like, 0.345627.

编辑:代码已修改。编辑 2:无法预测 n 的数量,d1,d2,...,dn 可能小于 1,例如 0.345627。

回答by Vaughn Cato

I think what you are interested in is error diffusion:

我认为您感兴趣的是误差扩散:

double d1 = x.xxxxxx;
double d2 = x.xxxxxx;
double d3 = x.xxxxxx;
double d4 = x.xxxxxx;
double d5 = x.xxxxxx;

double error = 0;
int i1 = (int)floor(d1+error+0.5);
error += d1-i1;
int i2 = (int)floor(d2+error+0.5);
error += d2-i2;
int i3 = (int)floor(d3+error+0.5);
error += d3-i3;
int i4 = (int)floor(d4+error+0.5);
error += d4-i4;
int i5 = (int)floor(d5+error+0.5);

int i = i1+i2+i3+i4+i5;
double d = d1+d2+d3+d4+d5;

Each time you round the value, you see how much error is introduced, and propagate a correction to the next calculation. This way, your error can never build up.

每次取整值时,您都会看到引入了多少误差,并将修正传播到下一次计算。这样,您的错误就永远不会累积。

回答by John Carter

now i needs to be not less then d - 0.5;

现在我需要不小于 d - 0.5;

This can be achieved easily by rounding the doubles up (cast to int will round them towards 0).

这可以通过四舍五入双精度轻松实现(转换为 int 会将它们四舍五入到 0)。

As Josua Green says, this can be done with (int)(d + 0.5)(if d>-0.5), or std::ceil()if you prefer.

正如 Josua Green 所说,这可以通过(int)(d + 0.5)(如果 d>-0.5)完成,或者std::ceil()如果您愿意。

回答by Michael J

Converting to an int truncates the double: i.e. drops any fraction bit.

转换为 int 会截断 double:即丢弃任何小数位。

By adding 0.5 to positive numbers and -0.5 to negative numbers, we get the more conventional behaviour.

通过将 0.5 添加到正数和 -0.5 到负数,我们得到了更传统的行为。

int ToInt(double x)
{
   double dx = x < 0.0 ? -0.5 : 0.5;
   return static_cast<int>(x + dx);
}

回答by Vincenzo Pii

Sum up the doubles:

双打总结:

double d = d1+d2+d3+d4+d5;

Check that the sum of doubles doesn't exceed the integer conversion by more than 0.5

检查双打的总和不超过整数转换超过 0.5

if (d - (int)d > 0.5) { /* Error? */ }