Thread: Extracting an exponent without knowing the size of the mantissa & exponent

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Extracting an exponent without knowing the size of the mantissa & exponent

    I've come up with this:
    Code:
    typedef union _pawkfdunion
    {
    	pawkfd num;
    	pawku  val;
    	pawcu  raw[sizeof(pawkfd)];
    } pawkfdunion;
    
    static pawvu pawkfd_man_len = 0;
    
    PAWSTR_API pawvu    pawkfdexp( pawkfd src )
    {
    	pawvu i = pawkfd_man_len;
    	pawkfdunion kfd = {0};
    	if ( !i )
    	{
    #	ifdef PAWKFD_INF
    		kfd.num = PAWKFD_INF;
    		kfd.num--;
    #	else
    		kfd.num = PAWKFD_MAX;
    #	endif
    		kfd.raw[sizeof(pawkfd)-1] &= PAWCD_MAX;
    		for ( ; kfd.val & 1u; ++i, kfd.val >>= 1 );
    		pawkfd_man_len = i;
    	}
    	kfd.num = src;
    	kfd.raw[sizeof(pawkfd)-1] &= PAWCD_MAX;
    	return (kfd.val >> i);
    }
    Assume pawku means __uint128, pawkfd means _Float128x & pawcu always means unsigned char, does anyone see any potential issues with this?
    Last edited by awsdert; 03-19-2023 at 09:41 AM. Reason: Used PAWCD_MIN instead of PAWCD_MAX

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by awsdert View Post
    ]
    Assume pawku means __uint128, pawkfd means _Float128x & pawcu always means unsigned char, does anyone see any potential issues with this?
    What does it do with denormalized numbers? Does it handles non-numbers like infinity?

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by hamster_nz View Post
    What does it do with denormalized numbers? Does it handles non-numbers like infinity?
    This function is just to extract the exponent so that I can construct a string from the float, because I'm doing this for a custom library that is to work with or without libc I have to avoid relying on external libraries which in all likelihood rely on libc anyways, in other words I have to rely ONLY on what the system and the compiler provide me with, unfortunately I can't find any predefines or builtins or otherwise that would provide me the exponent & mantissa width directly so calculating it with the assumption that the exponent is one lower than it's max value is the only way I can think of to obtain the mantissa length and in turn the exponent width which in turn allows me to get the exponent itself

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. mantissa, sign, exponent
    By -EquinoX- in forum C Programming
    Replies: 28
    Last Post: 03-05-2008, 02:25 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