I am implementing a Runge–Kutta procedure, which includes several time-critical multiplications with fixed, complicated fractions (which are not magic numbers but inherent to the algorithm) and I want this multiplications to be performed as efficient as possible whilst keeping the code readable.
For simplicity’s sake, let’s assume my code would look like the following, if I did not need to care about efficiency:
for (int i = 0; i < n; i++)
a[i] += f(i) + b[i] * (2197/4104.);
Can I assume that every reasonable compiler (with optimisation) will effectively replace 2197/4104 by 0.535331…? If not, what are good ways to ensure this? Would defining a const double
suffice, for example?
(Note that I am not interested in other possibilities for optimisation of the above code – it’s really just an example.)