This is a general question, but since I mostly deal with gcc/g++/VStudio, I have tagged it as c/c++. The question came to my mind when I was messing around with optimization options. In simplest form, consider an arithmetic operation such as i / 6 * 8
. If a human faces this expression, he will most likely simplify it to something like i / 3 * 4
. And if he is more comfortable with multiplying by 4, he will first do that, i.e. (i * 4) / 3
. I have to emphasize again that this is just a simple example.
Now what about the compilers? Are there any possibilities that they do the same with such operations? And since we know that in the above example, if i
is an integer, then simplifying and changing the order of operations may lead to completely different results, the question can be changed to: do the compilers completely avoid such actions?
If we want the program to do some arithmetic operations exactly as we stated and without changing the order of operations, should we be worried about the compiler's behavior?