Thread: Get Exponent of Float

1. Originally Posted by ZTik
The problem to solve was to avoid using Math header, library function and the shift-mask operators.
In that case the only option was to use the division and the remainder % operators.
Or just use addition...

Code:
```#include <stdio.h>
int main(int argc, char *argv[]) {
float f;
int x;
int e = 0;

if(sizeof(int) != 4 || sizeof(float) != 4) {
printf("Ints are of wrong size\n");
return 0;
}

printf("Enter value: ");
scanf("%f",&f);
printf("Value is %f\n",f);
x = *((int *)&f);

x += x; e = (x < 0 ? e*2+1 : e*2);
x += x; e = (x < 0 ? e*2+1 : e*2);
x += x; e = (x < 0 ? e*2+1 : e*2);
x += x; e = (x < 0 ? e*2+1 : e*2);
x += x; e = (x < 0 ? e*2+1 : e*2);
x += x; e = (x < 0 ? e*2+1 : e*2);
x += x; e = (x < 0 ? e*2+1 : e*2);
x += x; e = (x < 0 ? e*2+1 : e*2);

e -= 127;  // Remove exponent bias
printf("e is %i\n",e);
}```

2. For now I will stick to the division implementation, the addition one is efficient.

Popular pages Recent additions