c++typesfloating-pointterminologynumeric-limits

What maximal integer, capable of adding 1 a float type can hold in c++?


Suppose I am using float to hold integer values and adding small shifts to it, approximately 1s or 2s. At which value float will stop to change? What is the name of this value?


Solution

  • The smallest positive value of an IEEE 754 floating-point variable a where you get a == a+1 is 2^bits_precision, where bits_precision is one more than the number of bits in the significand and can be found with std::numeric_limits<T>::digits.

    For a 32-bit float, that's 24; for a 64-bit double, that's 53 (again, in the very common context of IEEE 754).

    Demo