Thread: Get Exponent of Float

  1. #16
    Registered User
    Join Date
    Sep 2020
    Posts
    223
    Quote Originally Posted by ZTik View Post
    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. #17
    Registered User
    Join Date
    May 2021
    Posts
    17
    For now I will stick to the division implementation, the addition one is efficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using an Exponent in C++
    By NismoT in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2011, 09:11 AM
  2. Does anyone know how to do exponent with just addition?
    By cowboyz209 in forum C Programming
    Replies: 11
    Last Post: 05-03-2011, 02:26 PM
  3. Some Exponent Help
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-04-2005, 07:31 PM
  4. Exponent help
    By flatline911 in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 01:34 AM
  5. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 02:24 PM

Tags for this Thread