0

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.

Antzi
  • 11,625
  • 6
  • 40
  • 66
  • 1
    I'm not so sure about the dup. While it does gives away a difference between multiplication and division, it does not include and advantage to use multiplication vs division. – Antzi Jul 22 '16 at 02:13
  • (apart from "I'm trying to do my compiler optimization job") – Antzi Jul 22 '16 at 02:15
  • If you're not satisfied with the dupe, improve and elaborate your question. – πάντα ῥεῖ Jul 22 '16 at 02:21
  • 3
    Some answers state that multiplication by inverse and division might give different results due to precision of float. So, it is not just optimization. – Jarod42 Jul 22 '16 at 02:46
  • When division by exact powers of 2 constants are involved, both / and * by reciprocal are equivalent. Otherwise, rounding will give different results for both, and compilers will NOT optimize the / even at O3 unless the "fast" or "unsafe" optimizations are enabled (and doing so for a whole file is blunt and possibly dangerous if you require exact IEEE 754 semantics to be obeyed elsewhere in it). So the multiplication *orders* the compiler to use this different result, because the programmer judges the loss of accuracy to be acceptable, and the compiler cannot make this determination itself. – Iwillnotexist Idonotexist Jul 22 '16 at 03:45
  • "(apart from "I'm trying to do my compiler optimization job")" -> While that's most of it, there's a coding consistency issue. It would be wierd to code division by constant as division for some constants and multiplication for others. Using division should be a signal in the code that we *really do need* a division there. – Iwillnotexist Idonotexist Jul 22 '16 at 03:57
  • Another common reason for explicitly spelling out optimizations in general is that an unfortunate number of popular modern compilers are frankly _awful_ at generating code at intermediate and debugger-friendly optimization levels. This may then results in impractically slow or large unoptimized debug builds, consider an embedded real-time application with a hard time/space envelope regardless of the build, unless some optimization is applied by hand. – doynax Jul 22 '16 at 08:26

1 Answers1

1

For most cases this just comes down to personal preference or general coding style. Say we are changing values a lot just to fiddle around with the output values when messing with a function to see how it behaves. Then, since the values may change, multiplying by a float makes it easier to just change to a number that wouldn't make sense to write as division.

Also, one advantage to multiplying by a float is it can help avoid the truncation towards zero that can mistakenly happen when dividing two ints.