I've created an object that acts somewhat like infinity for long ints. Specifically:
#ifndef MU_INF_H
#define MU_INF_H
#include "mu.h"
namespace mu {
class Inf {
public:
bool operator> ( long int i ) { return true; }
bool operator> ( Inf i ) { return false; }
... lots of other boolean operators ...
Inf& operator+ ( long int i ) { return *this; }
Inf& operator+ ( Inf i ) { return *this; }
... lots of other integer operators ...
}; // class Inf
} // namespace mu
#endif
And this all works pretty well, allowing me to run unit tests of the form:
mu::Inf inf;
long int n = -1;
long int z = 0;
long int p = 1;
ASSERT((inf + inf) == inf);
ASSERT((inf + n) == inf);
ASSERT((inf + z) == inf);
ASSERT((inf + p) == inf);
ASSERT((inf > inf) == false);
ASSERT((inf > n) == true);
ASSERT((inf > z) == true);
ASSERT((inf > p) == true);
At the risk of making it impossible to assign a checkmark, I have three questions:
static const
because it's not a "simple" object. What's the right approach: global? singleton pattern? ASSERT((1 + inf) == inf)
? (I won't be too sad if there isn't.)Infinity is defined as something that has no end, therefore is not represented as an integer. We know that all arithmetic operations performed on an infinite value will give an infinite value.
Representing infinity as an Integer in python One can use float('inf') as an integer to represent it as infinity.
isFinite is a function property of the global object. You can use this function to determine whether a number is a finite number. The isFinite function examines the number in its argument. If the argument is NaN , positive infinity, or negative infinity, this method returns false ; otherwise, it returns true .
To implement infinity in Java, we can simply divide the number by zero as shown below in the example. That's all for today, please mention in the comments in case you have any questions related to how to assign infinity in Java.
The infinity symbol ∞ is sometimes called the lemniscate and is a mathematical symbol representing the concept of infinity. The sign of infinity is used more often to represent a potential infinity, rather than to represent an actually infinite quantity such as the ordinal numbers and cardinal numbers.
static const Inf kInfinity;
works and will use the default constructor.
operator+
should be a free function that returns by value:
Inf operator+(Inf a, Inf b) { return a += b; }
You indicated that you would prefer to return a reference to kInfinity
instead of a value. This is possible (although it seems a bit unwieldy to me); a const
reference would have to be returned of course, since kInfinity
is const
.
Not that I'm aware of, although it seems to me that you are using references to Inf
and actual objects in a messy manner in your overloads.
Normally, you take arguments by value or const
reference and return by value for all operators besides the compound assignment ones (where you return by reference) to obtain the expected semantic. Of course, since your Inf object has no state, this all makes sense only to a certain extent.
I'd use a const
global to avoid the parentheses and the potential function call involved in a singleton. Whether this is also static
should make almost no difference (you aren't accessing this
in any way).
You have to write your operator as a free function:
inline Inf operator+(long int i, const Inf&) { return *this;}
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