Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using increment (operator++) on floats bad style?

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.

like image 578
Muxecoid Avatar asked Dec 19 '11 12:12

Muxecoid


People also ask

Can we use increment operator on float?

The ++ and += operators work just fine on float values.

Can ++ be used on float?

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.

Why do we use increment operator?

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.

Why doesn't Python have an increment operator?

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.


4 Answers

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.

like image 122
Sebastian Dressler Avatar answered Sep 26 '22 03:09

Sebastian Dressler


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.

like image 40
parapura rajkumar Avatar answered Sep 24 '22 03:09

parapura rajkumar


There is nothing wrong with using ++ and -- on float or double operands. It simply adds or subtracts 1. That's what it's for!

like image 22
TonyK Avatar answered Sep 27 '22 03:09

TonyK


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.

like image 9
Fred Foo Avatar answered Sep 23 '22 03:09

Fred Foo