Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

C++: Mantissa and Exponent of Floating Point Number

📅 2011-Oct-29 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, exponent, floating point, mantissa ⬩ 📚 Archive

The little-known frexp function from the C/C++ standard library can be used to extract the mantissa and exponent of a floating point number (float, double or long double):

#include <cmath>

const double d = 0.123;
int exponent;
const double mantissa = frexp( d, &exponent );

// Double:   0.123
// Mantissa: 0.984
// Exponent: -3

A few notes:


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧