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
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.
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.
Use Negative Product of numeric_limits::infinity() in C++
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".
#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';
}
i = 2147483645
i + 1 = not-a-number
Infinity looks like: +infinity
infinity + any integer = -infinity
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With