Compile-time trigonometry in C

I currently have code that looks like

while (very_long_loop) {
    ...
    y1 = getSomeValue();
    ...
    x1 = y1*cos(PI/2);
    x2 = y2*cos(SOME_CONSTANT);
    ...
    outputValues(x1, x2, ...);
}

an obvious optimization would be to calculate cosines ahead of time. I could do this by populating the array with values, but I was wondering if it is possible to get the compiler to compute them at compile time?

Edit: I know that C does not have a compile-time estimate, but I was hoping there would be some weird and ugly way to do this with macros.

+5
source share
8 answers

, : (, , ).

, , , .

+6

. . , Cosine ( Sine) .

, .

+1

, .

( script), ( #include ), .

: cos (pi/2) = 0!

+1

, cos , . , . , () - .

+1

, .

0

, C , double. - , . , C pi, .

0

, , .

, , , , . , , . , , , , .

0

Christoph .

, gcc . (, ), C C ( - ), . eRuby , (ePerl , .

make .eruby ( .eperl - ) , , , module.c.eruby module.h.eruby, make , module.c module.h, , . make , .

Windows - , , , IDE. , , make IDE, .eruby ( - ).

By the way, I saw that with incredibly small lines of code, I saw that eLua implemented the use of Lua as a macro language. Of course, any other scripting language that supports regular expressions and flexible layout rules should also work (but Python is reduced to this goal due to strict space rules).
0
source

All Articles