Cloning Python standard library's function math.frexp() that extracts exponent and mantissa from a floating point number
1. Review


The ctypes module is dangerous (it's possible to crash Python if you get it wrong) and so it should be used as a last resort, when no other facilities will do the job. In this case we could use the struct module instead:

&gt;&gt;&gt; # Reinterpret double-precision float as a 64-bit unsigned integer
&gt;&gt;&gt; struct.unpack('Q', (struct.pack('d', 5478324268.848498)))
(4752538446597034867,)

The code in the post gets at the bits in the representation by converting to a string of 0s and 1s. It would be better to use Python's bitwise operators instead, to avoid converting to a string and back again. Let's start by declaring named constants for the shifts and masks:

# IEEE-754 double-precision floating-point format
SIGN_SHIFT = 63
EXPONENT_SHIFT = 52
EXPONENT_MASK = (1 &lt;&lt; 11) - 1
MANTISSA_MASK = (1 &lt;&lt; 52) - 1


and then

Q, = struct.unpack('Q', (struct.pack('d', 5478324268.848498)))
sign = Q &gt;&gt; SIGN_SHIFT
biased_exponent = (Q &gt;&gt; EXPONENT_SHIFT) &amp; EXPONENT_MASK
mantissa = Q &amp; MANTISSA_MASK

To assemble the result, normalize the exponent and reverse the disassembly process:

NORMAL_EXPONENT = 1023
R = (sign &lt;&lt; SIGN_SHIFT) + (NORMAL_EXPONENT &lt;&lt; EXPONENT_SHIFT) + mantissa
normalized, = struct.unpack('d', struct.pack('Q', R))
return normalized, biased_exponent - NORMAL_EXPONENT


(Use 1022 instead of 1023 if you want to match math.frexp exactly.)


2. Revised code

from math import isinf, isnan

# IEEE-754 double-precision floating-point format
SIGN_SHIFT = 63
EXPONENT_SHIFT = 52
EXPONENT_MASK = (1 &lt;&lt; 11) - 1
MANTISSA_MASK = (1 &lt;&lt; 52) - 1
NORMAL_EXPONENT = 1023

def frexp(x):
    """Return the mantissa and exponent of x, as pair (m, e).
    m is a float and e is an int, such that x = m * 2.**e.
    If x is 0, m and e are both 0.  Else 1.0 &lt;= abs(m) &lt; 2.0.

    """
    x = float(x)
    if x == 0.0 or isinf(x) or isnan(x):
        return x, 0
    Q, = struct.unpack('Q', struct.pack('d', x))
    sign = Q &gt;&gt; SIGN_SHIFT
    biased_exponent = (Q &gt;&gt; EXPONENT_SHIFT) &amp; EXPONENT_MASK
    mantissa = Q &amp; MANTISSA_MASK
    R = (sign &lt;&lt; SIGN_SHIFT) + (NORMAL_EXPONENT &lt;&lt; EXPONENT_SHIFT) + mantissa
    normalized, = struct.unpack('d', struct.pack('Q', R))
    return normalized, biased_exponent - NORMAL_EXPONENT


(Note that this doesn't get denormals right. I could fix it with some more work, but I think I'd rather just use math.frexp.)

3. Use cases for frexp

frexp is useful when computing mathematical functions, where we need to get the argument into a known range in order to avoid overflow, or to use a particular solution technique. Two examples:


To compute \$\log a\$ we can use frexp to find \$a = m2^e\$ with \${1\over2} \le m &lt; 1\$ and then $$\log a = \log m2^e = \log m + e\log 2$$ and since \$m\$ is close to 1 we can use polynomial approximation (and \$\log 2\$ can be hard-coded).
The geometric mean of \$a_1, a_2, \dots, a_n\$ is \$\sqrt[n]{a_1a_2\dotsb a_n}\$ but the multiplication might overflow the floating-point range. Using frexp we can replace \$a_i\$ by \$m_i2^{e_i}\$ which gives us $$ \eqalign{ \sqrt[n]{a_1a_2\dotsb a_n} &amp;= \sqrt[n]{m_12^{e_1}m_22^{e_2}\dotsb m_n2^{e_n}} \\ &amp;= 2^{(e_1 + e_2 + \dotsb + e_n)/n}\sqrt[n]{m_1m_2\dotsb m_n}} $$ where \${1\over2} \le m_i &lt; 1\$. We can now divide the multiplication into chunks of length 1023 or less, knowing that each chunk can be multiplied and remain in range. We then use frexp on the results of the chunks to extract some more powers of 2, and repeat.

(An alternative approach to computing the geometric mean while keeping the intermediate results in range would be to use logarithms and exponentiation: $$ \sqrt[n]{a_1a_2\dotsb a_n} = \exp\left({\log a_1 + \log a_2 + \dotsb + \log a_n} \over n\right) $$ but computing a logarithm is much more expensive than multiplying.)