C++ long double bigger "safe" whole number

Viewed 255

IEEE 754 numbers are not uniformly spaced, the larger the numbers, the bigger the difference between two consecutive representable numbers. I have that the size of my long double with C++ is 16 bytes. So what is the bigger "safe" whole number "n" that can be represented with this type?. I call it "safe" if n - 1 is possible to represent but n + 1 not.

3 Answers

The IEEE 754 standard defines the parameters of various numerical types:
https://en.wikipedia.org/wiki/IEEE_754 for long doubles which are 128 bits long, the mantissa (the part of the floating point number that contains the significant digits) is 113 bits so it can represent integers with full precision up to 2^113 - 1. It can represent floating point numbers which are larger, but you start losing precision beyond that because the lower order numbers just get rounded.

As I understand, you're asking for which is the largest contiguous precisely representable integer. It is exactly:

std::pow(std::numeric_limits<long double>::radix, std::numeric_limits<long double>::digits)

or expressed in math: radixdigits where (quoted from cppreference)

The value of std::numeric_limits<T>::radix is the base of the number system used in the representation of the type. It is 2 for all binary numeric types, but it may be, for example, 10 for IEEE 754 decimal floating-point types ...

The value of std::numeric_limits<T>::digits is the number of digits in base-radix that can be represented by the type T without change. ... For floating-point types, this is the number of digits in the mantissa

In C++ you can get a lot of information using <limits>. For example:

#include <limits>
#include <iostream>
#include <iomanip>

int main() {
  auto p = std::numeric_limits<long double>::max_digits10;
  std::cout << "Max Long Double: "
            << std::setprecision(p)
            << std::setw(p + 7)
            << std::numeric_limits<long double>::max()
            << std::endl;
}

Prints:

Max Long Double: 1.18973149535723176502e+4932
Related