Is there a standard manner to determine the mantissa of a double?
You're willing to accept a Linux-specific solution, but you claim that glibc's ieee754.h
header does not satisfy your needs, so I conclude that the problem you are trying to solve is not extracting or conveying the bits themselves, as that header's union ieee_double
would provide a means for you to do that.
I read "the mantissa" as a different thing from "the number of bits of mantissa", so I conclude that DBL_MANT_DIG
of float.h
is not what you're looking for, either.
The only other thing I can think of that you might mean is the value of the significand (mantissa), according to the standard floating point model:
v = (sign) * significand * radixexponent
The frexp()
function, in the C language standard since C99, serves this purpose.1 It separates a double
into an exponent (of 2) and a significand, represented as a double
. For a finite, nonzero input, the absolute value of the result is in the half-open interval [0.5, 1).
Example:
#include <math.h>
#include <stdio.h>
void print_parts(double d) {
int exp;
double significand = frexp(d, &exp);
printf("%e = %f * 2^%d\n", d, significand, exp);
}
Sample outputs:
7.256300e+16 = 0.503507 * 2^57
1.200000e-03 = 0.614400 * 2^-9
-0.000000e+00 = -0.000000 * 2^0
Note that although the example function does not print sufficient decimal digits to convey the significands exactly, frexp()
itself is exact, not subject to any rounding errors.
1 Technically, frexp()
serves the purpose provided that FLT_RADIX
expands to 2. It is well-defined in any case, but if your double
representation uses a different radix then the result of frexp()
, though well-defined, is probably not what you're looking for.