Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify unbounded limit for numeric_limit<T>::max()?

I have an arbitrary precision Integer class, which is a lot like Java's BigInteger or OpenSSL's BIGNUM in operation. I'm having trouble understanding how I should express an unbounded limit for numeric_limit<Integer>::max().

Stack Overflow has a couple of question that asks if its OK to do (like Is it ok to specialize std::numeric_limits for user-defined number-like classes?), and some answers with some examples using primitives, but I did not see an example using an arbitrary precision integer class. I also visited std::numeric_limits reference page but its not clear to me what I should do in this situation.

At this point, my takeaway is its OK to specialize numeric_limit for my Integer, and its OK to put it in the standard namespace. I also need to specialize all numeric_limits.

How do I specify an unbounded limit for numeric_limit<T>::max()?


Below is from GCC 4.2.1's <limits> (OS X machine).

/// numeric_limits<int> specialization.
template<>
  struct numeric_limits<int>
  {
    static const bool is_specialized = true;

    static int min() throw()
    { return -__INT_MAX__ - 1; }
    static int max() throw()
    { return __INT_MAX__; }

    static const int digits = __glibcxx_digits (int);
    static const int digits10 = __glibcxx_digits10 (int);
    static const bool is_signed = true;
    static const bool is_integer = true;
    static const bool is_exact = true;
    static const int radix = 2;
    static int epsilon() throw()
    { return 0; }
    static int round_error() throw()
    { return 0; }

    static const int min_exponent = 0;
    static const int min_exponent10 = 0;
    static const int max_exponent = 0;
    static const int max_exponent10 = 0;

    static const bool has_infinity = false;
    static const bool has_quiet_NaN = false;
    static const bool has_signaling_NaN = false;
    static const float_denorm_style has_denorm = denorm_absent;
    static const bool has_denorm_loss = false;

    static int infinity() throw()
    { return static_cast<int>(0); }
    static int quiet_NaN() throw()
    { return static_cast<int>(0); }
    static int signaling_NaN() throw()
    { return static_cast<int>(0); }
    static int denorm_min() throw()
    { return static_cast<int>(0); }

    static const bool is_iec559 = false;
    static const bool is_bounded = true;
    static const bool is_modulo = true;

    static const bool traps = __glibcxx_integral_traps;
    static const bool tinyness_before = false;
    static const float_round_style round_style = round_toward_zero;
};
like image 980
jww Avatar asked Dec 23 '16 15:12

jww


People also ask

What is Numeric_limits int >:: max ()?

std::numeric_limits::max(): The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T.

What is Numeric_limits int min ()?

numeric_limits::min Returns the minimum finite value representable by the numeric type T . For floating-point types with denormalization, min returns the minimum positive normalized value.

What does Numeric_limits mean in C++?

std::numeric_limits ::digits in C++ with Example Return Value: The function std::numeric_limits<T>::digits returns the number of radix digits that the type can represent without loss of precision.


2 Answers

The standard says the following about the max member function in §18.3.2.4:

Meaningful for all specializations in which is_bounded != false.

So you should make is_bounded false and specify in the documentation for your class that it does not make sense to call std::numeric_limits<T>::max on it.

like image 138
Christian Hackl Avatar answered Oct 23 '22 18:10

Christian Hackl


concerning this question:

At this point, my takeaway is its OK to specialize numeric_limit for my Integer

The answer is Yes because:

  1. it's a specialisation of a standard template, and

  2. it's specialising for a user-defined type.

From cppreference:

It is allowed to add template specializations for any standard library template to the namespace std only if the declaration depends on a user-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.

like image 20
Richard Hodges Avatar answered Oct 23 '22 18:10

Richard Hodges