✅ Does arithmetic in a `const float` gets recomputed each time you access it?
I know this is a bit dummy question but just want to confirm whether it would be automatically handled by the compiler.
5 Replies
no (it does not get computed each time)
Alright thank you
When you declare a const float and initialize it with a value or an arithmetic expression, the value is computed at compile-time, and it remains constant throughout the program. The compiler treats constant expressions as fixed values, and it may optimize the code further by replacing the constant variable with its value directly in the compiled code.
Here's an example:
In this example, doublePi will be calculated at compile-time, and its value will be stored as a constant. Each time you access doublePi, the value will not be recomputed – it will simply return the precomputed constant value.
I see, thanks for clarifying this for me 👍
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.