I implement the Runge-Kutta procedure, which includes several time-critical multiplications with fixed complex fractions (which are not magic numbers, but inherent in the algorithm), and I want these multiplications to be executed as efficiently as possible, readable code.
For simplicity, let's assume that my code looks like this if I don't need to care about efficiency:
for (int i = 0; i < n; i++)
a[i] += f(i) + b[i] * (2197/4104.);
Is it possible to assume that every reasonable compiler (with optimization) will effectively replace 2197/4104 with 0.535331 ...? If not, what are some good ways to do this? Could a definition const double, for example?
(Note that I'm not interested in other options for optimizing the above code - this is really just an example.)
source
share