Nelson H. F. Beebe wrote:
Jim Cromie <jim.cromie@xxxxxxxxx> asks on Wed, 03 May 2006 12:17:38 -0400:
...
Are there any macros that can tear into a floating point number and
pull out the exponent and mantissa ?
...
Yes, the C89 and C99 ISO C Standards include ldexp(x,n), which forms x
* 2**n, and "f = frexp(x,&n);", which returns the fraction as a value
in [1/2,1) (or 0 if x is zero) and the exponent of 2 in n. Both are
exact, and can be implemented reasonably efficiently.
thank you for the thorough answer.
yes, frexp() is almost what I want.
heres basically what I tried to do with it:
#include <stdio.h>
#include <math.h>
#include <float.h>
/* evaluate these constants at compile-time ?!? */
float f = 3.14159 * 2 * 100;
int myexp;
double mant = frexp(f,&myexp);
/* this confirms gcc's unwillingness to run libmath routines at
compile-time */
float ff = ldexp(2,8);
int main(int c, char** v)
{
printf("float %g mantissa %f exp %d \n", f, mant, myexp);
}
as Id feared, it wouldnt compile;
$ make fp-comp
cc -g fp-comp.c -o fp-comp
fp-comp.c:37: error: initializer element is not constant
make: *** [fp-comp] Error 1
(the code above is slightly edited for clarity - err line numbers dont
match, but result does ;-(
I also tried these additions to compile cmd:
-fkeep-static-consts -fmerge-constants -fmerge-all-constants
-fsingle-precision-constant
So it seems that the compiler just wont call the function for me at
compile-time,
and fold the constant into the code for me. Id love to hear differently.
tia
jimc