Go Down

Topic: Very strange behavior from floor() (Read 3133 times) previous topic - next topic

PeterH





floor(x*1000.) is also a constant, because it's a function - by definition a single valued mapping - with a constant argument;



How is it 'by definition a single valued mapping'?

Because the compiler does value propagation, and it knows in the previous line you assigned 8.0001 to x.  Now, if x had been a non-const static or global variable, and was initialized to 8.0001, it generally would have to assume that the value could vary, and call the floor function.




The compiler could reasonably work out that the arguments passed to it are compile-time constants. You seem to be saying that by definition the return value of a function with arguments known at compile time is also a compile-time constant. That's not generally true. In order to know whether it was true for a given function, you'd need to know how that function is implemented.
I only provide help via the forum - please do not contact me for private consultancy.

MichaelMeissner

#31
Aug 21, 2012, 06:28 pm Last Edit: Aug 21, 2012, 06:33 pm by MichaelMeissner Reason: 1

The compiler could reasonably work out that the arguments passed to it are compile-time constants. You seem to be saying that by definition the return value of a function with arguments known at compile time is also a compile-time constant. That's not generally true. In order to know whether it was true for a given function, you'd need to know how that function is implemented.

The ISO C standard explicitly gives the compiler license to know about all functions defined in the ISO C standard (section 7.1.4), and it is perfectly legitimate for the compiler to optimize calls.  Since the C++ language is built on top of C (with some changes, and many additions), I believe it has similar wording in its standard.  So since floor is an ISO C standard function (7.12.9.2 in ISO C99), the GCC compiler can optimize calls that the compiler knows are constant.

I should mention that I was one of the founding members of the original X3J11 ANSI C standard committee (for what would become the ANSI C89 and later ISO C90 standards) and it was an explicit choice of the committee that the compiler should be given freedom to do such optimizations.  As I mentioned earlier, GCC does have switches to turn off builtin function handling, but you would have to convince the Arduino IDE to specify that option.

PeterH


The ISO C standard explicitly gives the compiler license to know about all functions defined in the ISO C standard (section 7.1.4), and it is perfectly legitimate for the compiler to optimize calls.


If you had written "because it's a function which the compiler knew had a return value depending only on the arguments and with no other side effects" I'd have been happy. Without that condition you're asserting the same behaviour for functions in general, which I suspect is not what you intended.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up