[linux-dvb] slightly OT: Floats at compile time

Edgar Toernig froese at gmx.de
Sun Jan 15 02:09:26 CET 2006


Patrick Boettcher wrote:
>
> My question is: Is it save to assume, that all compilers calculate the 
> following assignment at compile time?
> 
> int value = 0.523 * 65535;

Gcc does it at compile time (as do most compilers).  Whether _all_?
I don't know.  There are pretty strange C compilers and afaics the
standard allows run-time computation.

However, gcc also computes

    #define value ((int)(0.523 * 65535))

at compile time.


But if you prefer the int var, IMHO better is:

    static const int value = 0.534 * 65535 + 0.5;

so that the compiler may optimize further arithmetic with <value>.


> Presuming it is true, would kernel guys accept such a thing: using 
> floats for compile time constants?

You'd better ask that on lkml.  There'll be at least one who will
have an objection ;-)

Ciao, ET.



More information about the linux-dvb mailing list