0

I would like to understand how floating-point values (float and double) are represented in ANSI C. That is, how many bits are used for representing the mantissa and the exponent of float and double values?

I'm not sure if ANSI C adopts IEEE 754.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Zaratruta
  • 2,097
  • 2
  • 20
  • 26
  • note that most mainstream platforms (e.g. x86-64, AARCH64) are IEEE 754 compatible, on these platforms `float` is a binary32 (i.e. 32bit in single-precision floating point format) while a `double` is a binary64 (i.e. 64bit double-precision). note that optional optimization levels (e.g. `-ffast-math` in GCC/Clang) can break compatibility, even though the bit-level representation is the same – Sam Mason Aug 05 '21 at 12:05

1 Answers1

0

C doesn't mandate any specific floating-point format. It doesn't even say that floating-point types must be in binary, so it's valid for an implementation to use decimal floats. In fact there are many old floating-point formats that use hexadecimal instead of binary

Just like integer types, C just specifies various limits of floating-point types like the radix or the number of digits in the significand and exponent parts like

  • FLT_RADIX
  • FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG
  • FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP
  • FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP
  • ...

So you can use them if you just want to know how many bits are used for representing the mantissa and the exponent of float and double values

For more details read C11's 5.2.4.2.2 Characteristics of floating types <float.h>

If you want to make sure that the implementation uses IEEE-754 then you can check it with __STDC_IEC_559__

See also

phuclv
  • 37,963
  • 15
  • 156
  • 475