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.
The ++ and += operators work just fine on float values.
C, C++, C# and many other programming languages recognize float as a data type. Other common data types include int and double. The float type can represent values ranging from approximately 1.5 x 10-45 to 3.4 x 1038, with a precision — the limit of digits — of seven.
1) Increment Operators: The increment operator is used to increment the value of a variable in an expression. In the Pre-Increment, the value is first incremented and then used inside the expression. Whereas in the Post-Increment, the value is first used inside the expression and then incremented.
Because, in Python, integers are immutable (int's += actually returns a different object). Also, with ++/-- you need to worry about pre- versus post- increment/decrement, and it takes only one more keystroke to write x+=1 . In other words, it avoids potential confusion at the expense of very little gain.
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.
++/--
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
.
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
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.
There is nothing wrong with using ++
and --
on float or double operands. It simply adds or subtracts 1. That's what it's for!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With