Is it possible to compute pow(10,x) at compile time?
I've got a processor without floating point support and slow integer division. I'm trying to perform as many calculations as possible at compile time. I can dramatically speed up one particular function if I pass both x
and C/pow(10,x)
as arguments (x and C are always constant integers, but they are different constants for each call). I'm wondering if I can make these function calls less error prone by introducing a macro which does the 1/pow(10,x)
automatically, instead of forcing the programmer to calculate it?
Is there a pre-processor trick? Can I force the compiler optimize out the library call?
You can use the scientific notation for floating point values which is part of the C language. It looks like that:
e = 1.602E-19 // == 1.602 * pow(10, -19)
The number before the E
( the E
maybe capital or small 1.602e-19
) is the fraction part where as the (signed) digit sequence after the E
is the exponent part. By default the number is of the type double
, but you can attach a floating point suffix (f
, F
, l
or L
) if you need a float
or a long double
.
I would not recommend to pack this semantic into a macro: