Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining an object that acts like integer infinity

Tags:

c++

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:

  • Does C++ already provide something like this, and/or is there an obvious better way than what I'm doing here?
  • I want to make an instance of Inf available throughout my system. I can't declare it a static const because it's not a "simple" object. What's the right approach: global? singleton pattern?
  • Is there a way to handle symmetric operators where the long int comes first, i.e. ASSERT((1 + inf) == inf)? (I won't be too sad if there isn't.)
like image 940
fearless_fool Avatar asked May 05 '14 05:05

fearless_fool


People also ask

Can infinity be an integer?

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.

How do you define infinity in Python?

Representing infinity as an Integer in python One can use float('inf') as an integer to represent it as infinity.

What is isFinite?

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 .

How do you convert integer to infinity in Java?

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.

How do you express infinity as a number?

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.


2 Answers

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.

like image 45
M.M Avatar answered Sep 24 '22 16:09

M.M


  1. 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.

  2. 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).

  3. You have to write your operator as a free function:

     inline Inf operator+(long int i, const Inf&) { return *this;} 
    
like image 187
Matteo Italia Avatar answered Sep 22 '22 16:09

Matteo Italia