C++ 浮点到 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3127962/
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
C++ float to int
提问by Michael Sync
Maybe, it's very simple question but I couldn't get the answer. I've been searching quite a while ( now Google think that I'm sending automated queries http://twitter.com/michaelsync/status/17177278608) ..
也许,这是一个非常简单的问题,但我无法得到答案。我已经搜索了一段时间(现在谷歌认为我正在发送自动查询http://twitter.com/michaelsync/status/17177278608)..
int n = 4.35 *100;
cout << n;
Why does the output become "434" instead of "435"? 4.35 * 100 = 435 which is a integer value and this should be assignable to the integer variable "n", right?
为什么输出变成“434”而不是“435”?4.35 * 100 = 435 这是一个整数值,这应该可以分配给整数变量“n”,对吗?
OR Does the C++ compiler cast 4.35 to integer before multiplying? I think it won't. Why does the compiler automatically change 4.35 to 4.34 which is still a float??
或 C++ 编译器是否在乘法之前将 4.35 转换为整数?我认为不会。为什么编译器会自动把 4.35 改成 4.34 还是浮点数??
Thanks.
谢谢。
回答by Nick Bastin
What Every Computer Scientist Should Know About Floating-Point Arithmetic
That's really just a starting point, sadly, as then languages introduce their own foibles as to when they do type conversions, etc. In this case you've merely created a situation where the constant 4.35 can't be represented precisely, and thus 4.35*100 is more like 434.9999999999, and the cast to int
does trunc
, not round
.
遗憾的是,这真的只是一个起点,因为当时的语言会在进行类型转换等方面引入自己的弱点。 * 100更像是434.9999999999,并投以int
不trunc
,不round
。
回答by Donnie
If you run this statement:
如果您运行此语句:
cout << 4.35
Dollars to donuts you get something approximately like 4.3499998821 because 4.35 isn't exactly representable in a float.
美元到甜甜圈你会得到大约像 4.3499998821 的东西,因为 4.35 不能完全用浮点数表示。
When the compiler casts a float to an int it truncates.
当编译器将 float 转换为 int 时,它会截断。
To get the behavior your expect, try:
要获得您期望的行为,请尝试:
int n = floor((4.35 * 100.0) + 0.5);
int n = floor((4.35 * 100.0) + 0.5);
(The trickyness with floor
is because C++ doesn't have a native round()
function)
(棘手的floor
是因为 C++ 没有本机round()
函数)
回答by Cogwheel
The internal representation of 4.35 ends up being 4.349999999 or similar. Multiplying by 100 shifts the decimal, and the .9999 is dropped off (truncated) when converting to int.
4.35 的内部表示最终是 4.349999999 或类似的。乘以 100 会移动小数点,而 .9999 在转换为 int 时会被删除(截断)。
Edit: Was looking for the link Nick posted. :)
编辑:正在寻找尼克发布的链接。:)
回答by Nicholas Knight
Floating point numbers don't work that way. Many (most, technically an infinite number of...) values cannot be stored or manipulated precisely as floating point. 4.35 would seem to be one of them. It's getting stored as something that's actually below 4.35, hence your result.
浮点数不能那样工作。许多(大多数,技术上是无限数量的……)值不能作为浮点精确存储或操作。4.35 似乎是其中之一。它被存储为实际上低于 4.35 的值,因此您的结果。
回答by CB Bailey
When a float
is converted to an int
the fractional part is truncated, the conversion doesn't take the nearest int
to the float
in value.
当float
被转换为int
小数部分被截断,转换不采取最接近int
于float
在值。
4.35 can't be exactly represented as a float
, the nearest representable number is (we can deduce) very slightly less that 4.35, i.e. 4.34999... , so when multiplied by 100 you get 434.999...
4.35 不能完全表示为 a float
,最接近的可表示数是(我们可以推断出)比 4.35 稍微小一点的数,即 4.34999... ,所以当乘以 100 时,您会得到 434.999...
If you want to convert a positive float
to the nearest int
you should add 0.5 before converting to int
.
如果要将正数转换为float
最接近的值int
,则应在转换为int
.
E.g.
例如
int n = (4.35 * 100) + 0.5;
cout << n;