Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent integer infinity?

Tags:

c#

infinity

I need a way to represent an integer number that can be infinite. I'd prefer not to use a floating point type (double.PositiveInfinity) since the number can never be fractional and this might make the API confusing. What is the best way to do this?

Edit: One idea I haven't seen yet is using int? with null representing infinity. Are there any good reasons not to do this?

like image 439
ConditionRacer Avatar asked Jan 23 '14 15:01

ConditionRacer


People also ask

How do you represent infinity in integers?

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

Is infinity an integer number?

Infinity is not an integer. It's not a real number either but it comes up more often in the kind of arithmetic that floating point numbers are used for (e.g. involving transcendental and trigonometric functions).

How do you code infinity?

Hold the ALT key and type 236 on the num-lock keypad. Hold the ALT key and type 236 on the num-lock keypad.

How do you represent infinity in JavaScript?

Infinity in JavaScript represents the concept of an infinite number. Any finite number is smaller than Infinity , and any finite number is bigger -Infinity . Comparing infinite values in JavaScript is easy: Infinity === Infinity is true . The special function Number.


Video Answer


2 Answers

If you don't need the full range of integer values, you can use the int.MaxValue and int.MinValue constants to represent infinities.

However, if the full range of values is required, I'd suggest either creating a wrapper class or simply going for doubles.

like image 101
BambooleanLogic Avatar answered Sep 19 '22 05:09

BambooleanLogic


An example partial implementation along the lines of the comments of SLaks and others (feedback welcome):

Usage:

int x = 4; iint pi = iint.PositiveInfinity; iint ni = iint.NegativeInfinity;  Assert.IsTrue(x + pi == iint.PositiveInfinity); Assert.IsTrue(pi + 1 == iint.PositiveInfinity); Assert.IsTrue(pi + (-ni) == iint.PositiveInfinity); Assert.IsTrue((int)((iint)5) == 5); 

Implementation:

public struct iint {     private readonly int _int;      public iint(int value)      {         if(value  == int.MaxValue || value == int.MinValue)             throw new InvalidOperationException("min/max value reserved in iint");         _int = value;     }      public static explicit operator int(iint @this)     {         if(@this._int == int.MaxValue || @this._int == int.MinValue)             throw new InvalidOperationException("cannot implicit convert infinite iint to int");          return @this._int;     }      public static implicit operator iint(int other)     {         if(other == int.MaxValue || other == int.MinValue)             throw new InvalidOperationException("cannot implicit convert max-value into to iint");         return new iint(other);     }      public bool IsPositiveInfinity {get { return _int == int.MaxValue; } }      public bool IsNegativeInfinity { get { return _int == int.MinValue; } }      private iint(bool positive)     {         if (positive)             _int = int.MaxValue;         else             _int = int.MinValue;     }      public static readonly iint PositiveInfinity = new iint(true);      public static readonly iint NegativeInfinity = new iint(false);      public static bool operator ==(iint a, iint b)     {         return a._int == b._int;     }      public static bool operator !=(iint a, iint b)     {         return a._int != b._int;     }      public static iint operator +(iint a, iint b)     {         if (a.IsPositiveInfinity && b.IsNegativeInfinity)             throw new InvalidOperationException();         if (b.IsPositiveInfinity && a.IsNegativeInfinity)             throw new InvalidOperationException();         if (a.IsPositiveInfinity)             return PositiveInfinity;         if (a.IsNegativeInfinity)             return NegativeInfinity;         if (b.IsPositiveInfinity)             return PositiveInfinity;         if (b.IsNegativeInfinity)             return NegativeInfinity;          return a._int + b._int;     }      public static iint operator -(iint a, iint b)     {         if (a.IsPositiveInfinity && b.IsPositiveInfinity)             throw new InvalidOperationException();         if (a.IsNegativeInfinity && b.IsNegativeInfinity)             throw new InvalidOperationException();         if (a.IsPositiveInfinity)             return PositiveInfinity;         if (a.IsNegativeInfinity)             return NegativeInfinity;         if (b.IsPositiveInfinity)             return NegativeInfinity;         if (b.IsNegativeInfinity)             return PositiveInfinity;          return a._int - b._int;     }      public static iint operator -(iint a)     {         if (a.IsNegativeInfinity)             return PositiveInfinity;         if (a.IsPositiveInfinity)             return NegativeInfinity;          return -a;     }      /* etc... */     /* other operators here */ } 
like image 28
Jack Avatar answered Sep 21 '22 05:09

Jack