I want to calculate number of mantissa bits in float and double. I know those numbers should be 23 and 52, but I have to calculate it in my program.
Asked
Active
Viewed 1,654 times
0
-
6Why do you need to calculate it in your program? – Ed Heal Mar 05 '16 at 12:36
-
Maybe you can start [here](https://en.wikipedia.org/wiki/Floating_point) – Nard Mar 05 '16 at 12:39
-
@EdHeal at a rough guess, because that's what the OP's prof/TA asked for. – Martin James Mar 05 '16 at 12:39
-
2Calculate it how..? By twiddling a float around and seeing how it changes? There are constants for these values in headers, if you don't want to hardcode magic numbers. – Matti Virkkunen Mar 05 '16 at 12:41
-
@MattiVirkkunen yeah, float-twiddling and bit-checking was all I could think of, if forced to find out in software what I could just look up in documentation.. – Martin James Mar 05 '16 at 12:48
-
For a more "C++" answer, see [this answer](https://stackoverflow.com/a/57201197/193789) – kebs Feb 22 '23 at 10:06
2 Answers
5
There are constants you can use defined in the header <cfloat>
See FLT_MANT_DIG for example.

Captain Giraffe
- 14,407
- 6
- 39
- 67
-
5Just remember to take FLT_RADIX into account, in case you might be running on a computer that doesn't use base-2 floats! – Matti Virkkunen Mar 05 '16 at 12:42
3
There is an ambiguity in number of mantissa bits: it could be
- the number of bits needed to represent the floating point value.
- the number of bits stored into the floating point representation.
Typically, the mantissa as stored in the IEEE floating point format does not include the initial 1
that is implied for all regular non zero numbers. Therefore the number of bits in the representation is one less that the true number of bits.
You can compute this number for the binary floating point formats in different ways:
- some systems define manifest contants
FLT_MANT_BITS
,DBL_MANT_BITS
andLDBL_MANT_BITS
. The value is the true number of mantissa bits. - you can derive the number of bits from a direct computation involving
FLT_EPSILON
defined in<float.h>
:FLT_EPSILON
is the smallest float value such that1.0f + FLT_EPSILON
is different from1.0f
. The true number of mantissa is1 - log(FLT_EPSILON) / log(2)
. The same formula can be used for other floating point formats. - you can compute the values with a loop, as illustrated in the code below.
Here is a test utility:
#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void) {
int n;
float f = 1.0;
for (n = 0; 1.0f + f != 1.0f; n++) {
f /= 2;
}
#ifdef FLT_MANT_BITS
printf("#define FLT_MANT_BITS %d\n", FLT_MANT_BITS);
#endif
#ifdef FLT_EPSILON
printf("1 - log(FLT_EPSILON)/log(2) = %g\n", 1 - log(FLT_EPSILON) / log(2));
#endif
printf("Mantissa bits for float: %d\n", n);
double d = 1.0;
for (n = 0; 1.0 + d != 1.0; n++) {
d /= 2;
}
#ifdef DBL_MANT_BITS
printf("#define DBL_MANT_BITS %d\n", DBL_MANT_BITS);
#endif
#ifdef DBL_EPSILON
printf("1 - log(DBL_EPSILON)/log(2) = %g\n", 1 - log(DBL_EPSILON) / log(2));
#endif
printf("Mantissa bits for double: %d\n", n);
long double ld = 1.0;
for (n = 0; 1.0 + ld != 1.0; n++) {
ld /= 2;
}
#ifdef LDBL_MANT_BITS
printf("#define LDBL_MANT_BITS %d\n", LDBL_MANT_BITS);
#endif
#ifdef LDBL_EPSILON
printf("1 - log(LDBL_EPSILON)/log(2) = %g\n", 1 - log(LDBL_EPSILON) / log(2));
#endif
printf("Mantissa bits for long double: %d\n", n);
return 0;
}
Output on my laptop:
1 - log(FLT_EPSILON)/log(2) = 24
Mantissa bits for float: 24
1 - log(DBL_EPSILON)/log(2) = 53
Mantissa bits for double: 53
1 - log(LDBL_EPSILON)/log(2) = 64
Mantissa bits for long double: 64

chqrlie
- 131,814
- 10
- 121
- 189
-
1"number of mantissa bits" , though not _precisely_ defined, should be +1 or `53` for double. The count should reflect the mathematics of the value which has a 53 bit significand even though with typical FP formats, only 52 are explicitly stored (1 implied). (C++, certain `DBL_MANT_DIG` --> 53 – chux - Reinstate Monica Mar 05 '16 at 16:50
-
1I agree, this notion is a bit confusing, a more thorough explanation was needed. I came up with a simpler computation too. – chqrlie Mar 05 '16 at 17:44