C++ 是否在浮动样式上使用增量(运算符++)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8561393/
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
Is using increment (operator++) on floats bad style?
提问by Muxecoid
Is it considered "bad style" to use the increment operator (++) on floats? It compiles just fine but I find it smelly and counter-intuitive.
在浮点数上使用增量运算符 (++) 是否被认为是“糟糕的风格”?它编译得很好,但我觉得它很臭而且违反直觉。
The question: In what cases is using ++
on float variable justified and better than += 1.0f
? If there are no use cases, is there a respectable C++ style guide that explicitly says that ++ on float is evil?
问题:在什么情况下使用++
浮点变量是合理的并且比使用更好+= 1.0f
?如果没有用例,是否有一个受人尊敬的 C++ 风格指南明确指出浮动上的 ++ 是邪恶的?
For float ++ does not increment by the smallest possble value, but by 1.0. 1.0f has no special meaning (unlike integer 1). It may confuse the reader causing him to think that the variable is int.
对于 float ++ 不会增加最小的可能值,而是增加 1.0。1.0f 没有特殊含义(与整数 1 不同)。这可能会使读者感到困惑,使他认为变量是 int。
For float it is not guaranteed that operator++ changes the argument. For example the following loop is not infinite:
对于 float ,不保证 operator++ 会更改参数。例如,以下循环不是无限的:
float i, j;
for (i=0.0, j=1.0; i!=j;i=j++);
Consequently doing ++ immediately after -- does not guarantee that the value is unchanged.
因此,在 -- 之后立即执行 ++ 并不能保证该值不变。
采纳答案by Sebastian Dressler
In general ++/--
is not defined for floats, since it's not clear with which value the float should be incremented. So, you may have luck on one system where ++
leads to f += 1.0f
but there may be situations where this is not valid. Therefore, for floats, you'll have to provide a specific value.
通常++/--
没有为浮点数定义,因为不清楚浮点数应该增加哪个值。因此,您可能在一个系统上很幸运++
,f += 1.0f
但在某些情况下这可能无效。因此,对于浮点数,您必须提供一个特定的值。
++/--
is defined as "increment/decrement by 1". Therefore this is applicable to floating point values. However, personally i think, that this can be confusing to someone who isn't aware of this definition (or only applies it to integers), so i would recommend using f += 1.0f
.
++/--
被定义为“递增/递减 1”。因此这适用于浮点值。但是,我个人认为,对于不了解此定义(或仅将其应用于整数)的人来说,这可能会造成混淆,因此我建议使用f += 1.0f
.
回答by parapura rajkumar
When you add a lots of 1.0
to a float, because of floating point arithmetic you might be a little off in the end
当您1.0
向浮点数添加很多时,由于浮点运算,您最终可能会有点偏离
The best way is to do
最好的方法是做
for ( int i = 0; i < 100; i++ )
{
float f = 2.433f + i * 1.0f;
instead of
代替
for ( float f = 2.433f; f < 102.433f; f += 1.0f )
In the second case the floating point arithmetic error adds up and in the first case it doesn't. As certain users have pointed out in comments below adding integrals floats might not accumulate errors but in general it is a good idea to avoid it.
在第二种情况下,浮点算术错误加起来,在第一种情况下不会。正如某些用户在下面的评论中指出的那样,添加积分浮点数可能不会累积错误,但总的来说,避免它是一个好主意。
回答by TonyK
There is nothing wrong with using ++
and --
on float or double operands. It simply adds or subtracts 1. That's what it's for!
在浮点数或双操作数上使用++
and没有任何问题--
。它只是加或减 1。这就是它的用途!
回答by Fred Foo
It's bad style. ++
and --
are intended to set an lvalue to its next or previous value, like the next or previous integer, the next or previous element in an array (for pointers), the next or previous element in a container (iterators), etc.
画风很差 ++
并且--
旨在将左值设置为其下一个或上一个值,例如下一个或上一个整数、数组中的下一个或上一个元素(对于指针)、容器中的下一个或上一个元素(迭代器)等。
Next and previous values are not well-defined for floats. Do f += 1.
explicitly.
对于浮点数,下一个和上一个值没有明确定义。做f += 1.
明确。