Reading code from a program I'm newly contributing to, I found out that division was almost never used, in favor or multiplication by floats.
One exemple would be when trying to average to floats such as:
float a = 0.42;
float b = 0.666;
float c = (a + b) * 0.5;
Clearly the intent is to divide by two to average a
and b
However, altho simple I find the use of * 0.5
slightly harmful for readability (especially semantic) compared to the following:
float c = (a + b) / 2;
Which produce the exact same result.
Is there any reason why I would want to use * 0.5
instead of / 2
in this case ?
Proposed duplicate indicate that multiplication is faster; which becomes obviously false if any optimisation level is used (And yes, we do compile with optimisations)
Question is about c++ but other languages answers could be helpful too.