I am writing a Simulation program and am wondering if using const double can be used while saving intermediate results. Consider this snippet:
double DoSomeCalculation(const AcModel &model) { (...) const double V = model.GetVelocity(); const double m = model.GetMass(); const double cos_gamma = cos(model.GetFlightPathAngleRad()); (...) return m*V*cos_gamma*Chi_dot; }
Please note that the sample is shown for illustration only - it may not make much sense on the technical side. The motivation for saving, for example, cos_gamma in a variable, is that this cosine is used many times in other expressions covered by (...), and I feel that the code becomes more readable when using
cos_gamma
but not
cos(model.GetFlightPathAngleRad())
in various terms. Now the real question is this: since I expect the cosine to be the same as in the code section, and I actually created the thing only as a placeholder, and for convenience I tend to declare it const. Is there a naked opinion that this is good or bad, or can he bite me at the end? Does the compiler help to use this additional information, or am I really preventing the compiler from performing useful optimizations?
Arne
source share