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?
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.
For float it is not guaranteed that operator++ changes the argument. For example the following loop is not infinite:
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.
See Question&Answers more detail:os