Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to implement a data type that would take integer values plus an infinity symbol in c++.

Tags:

c++

Then I would like to have the normal operations on the data type (addition , subtraction etc.). All operations on infinity are defined in the natural way. So for instance , infinity + any integer = infinity.

Of course , I could do this with a struct construct and then define all the operations. Is there a neat way to do this in c++.

Thank You

like image 900
vipulharsh Avatar asked May 26 '13 12:05

vipulharsh


People also ask

How do you convert an integer to infinity in C++?

Setting an int Infinity: The nearby value that we can get is by initializing an “int” to its extreme value. The closest we can get by setting a variable to the maximum value that is double “a = std: numeric_limits<int>:: max();”. Which would be 231-1 if it is 32 bits wide on our implementation.

What is the range of values that can be stored by INT datatype in C?

The INTEGER data type stores whole numbers that range from -2,147,483,647 to 2,147,483,647 for 9 or 10 digits of precision. The number 2,147,483,648 is a reserved value and cannot be used. The INTEGER value is stored as a signed binary integer and is typically used to store counts, quantities, and so on.

How do you assign a negative infinity to a variable in C++?

Use Negative Product of numeric_limits::infinity() in C++


2 Answers

Is there a neat way to do this in c++.

The good news is that you don't have to; this problem has already been solved (and tested) in boost::date_time::int_adapter.

I've no idea why it's in date_time, but that particular class template is an adapter to create integer types with ±∞, and "not a number".

Sample program:

#include <boost/date_time/int_adapter.hpp>
#include <iostream>

int main()
{
    typedef boost::date_time::int_adapter<int> integer;
    integer const i = integer::max();
    std::cout << "i = " << i << '\n';
    std::cout << "i + 1 = " << i + 1 << '\n';
    std::cout << "Infinity looks like: " << integer::pos_infinity() << '\n';
    // So for instance , infinity + any integer = infinity.
    std::cout << "infinity + any integer = " << integer::neg_infinity() + 1 << '\n';
}

Sample output:

i = 2147483645
i + 1 = not-a-number
Infinity looks like: +infinity
infinity + any integer = -infinity
like image 146
Johnsyweb Avatar answered Sep 22 '22 17:09

Johnsyweb


If you define an implicit conversion operator and constructor to convert to / from the wrapped type (in your case I guess you mean int), all arithmetic operations work as expected on the wrapped value. Something like this:

class Infinity {}; // Empty helper class, see second constructor

class MaybeInfinity {
    int value;
    bool infinity;

public:
    MaybeInfinity(int value = 0) : value(value), infinity(false) {}
    MaybeInfinity(Infinity) : value(0), infinity(true) {}

    bool isInfinity() const { return infinity; }

    const int & operator() const { return value; }
    int & operator() { return value; }
    ...
};

However, you say you want to define custom behavior for (some of) the arithmetic operations. Then you're best with overloading all arithmetic operators. For example, the addition can then be written as:

class MaybeInfinity {
    ...
    MaybeInfinity operator +(const MaybeInfinity & other) const {
        if (infinity || other.infinity) {
            return Infinity();
        }
        return value + other.value;
    }
    ...
};

Note that for all operators for which you don't overload a particular arithmetic operator, your class behaves like normal integer arithmetic, thanks to the conversion operators. Also, you can calculate with your class and integer values, like:

MaybeInfinity number = 3;
number += 2;
MaybeInfinity otherNumber = Infinity();
number += otherNumber;
// and so on

PS: This class can be a template. Replace int by T, prepend the definition with template<typename T>, make sure you don't separate implementation in a .cpp file, then use the type like MaybeInfinity<int> or use other wrapped types.

like image 26
leemes Avatar answered Sep 20 '22 17:09

leemes