GameX
GameX

Reputation: 67

C++ datatype s and their range

Do datatypes exist in C++ with range

1 <= N <= 1018
0 <= K <= 1018

If not, is there anyway to restrict a variables input range?

Upvotes: 0

Views: 97

Answers (2)

Compuholic
Compuholic

Reputation: 388

If you need to limit the range you can wrap the variable in a class. You can overload the operators so that you can do arithmetic with the value as you would normally do. Usually I would resort to templates to implement such functionality but I think the example below is easier to understand.

class MyInt
{
    public:
    MyInt(int minval, int maxval);

    MyInt& operator=(MyInt const& rhs);
    MyInt& operator=(int rhs);

    private:
    int _val;
    int _minval;
    int _maxval;

    bool _is_inrange(int val);
};

Whenever you perform an operation on the class it needs to check whether the result is in the correct range. Of course the data type you base your class on needs to be able to accomodate the desired value range. I used int as an example. If you use templates you could set select the base data type when instantiating the class.

MyInt::MyInt(int minval, int maxval)
{
    _minval = minval;
    _maxval = maxval;
}

bool MyInt::_is_inrange(int val)
{
    return _minval <= val && val < _maxval;
}

You can overload the operators to work with the values in the same way you are working with primitive datatypes. I have implemented the assignment operator as an example but you can also overload the arithmetic operators.

MyInt& MyInt::operator=(int rhs)
{
    if (_is_inrange(rhs))
    {
        _val = rhs;
    }
    else
    {
        // throw an error or do something else.
        cout << "Error: Invalid value" << endl;
    }
    return *this;
}

MyInt& MyInt::operator=(MyInt const& rhs)
{
    if (_is_inrange(rhs._val))
    {
        _val = rhs._val;
    }
    else
    {
        // throw an error or do something else.
        cout << "Error: Invalid value" << endl;
    }
    return *this;
}

And finally here is how you would use that class in a program.

int main()
{
    MyInt custom_int(0, 10);
    cout << "Assigning valid value..." << endl;
    custom_int = 9;

    cout << "Assigning invalid value..." << endl;
    custom_int = 10;

    return 0;
}

Upvotes: 1

Jeremy Friesner
Jeremy Friesner

Reputation: 73081

Since 1018 < 264, unsigned long long will be big enough to hold values in your requested ranges.

Regarding "restricting a variable's input range", it's not clear what kind of restrictions you have in mind:

  • Do you want functionality such that values declared outside the range fail to compile?
  • Do you want functionality such that if value is computed that falls outside that range, some special action (such as crashing the program, or throwing an exception, or printing an error message) occurs?
  • Or are you looking for a datatype that clamps "out of range" values to the nearest value in the range?
  • Or are you looking for a datatype that acts like C++'s build-in unsigned datatypes, where overflow (and underflow) cause a modulo-style "wrap-around" in the represented value?

Some of those handling strategies could be implemented via a custom class (at the cost of a certain amount of efficiency). If you don't need any particular error-checking for out-of-range values, OTOH, then plain old unsigned long long will work fine and be most efficient as it maps directly to the underlying CPU hardware.

Upvotes: 1

Related Questions