Class InfinityClass
In: lib/more/facets/infinity.rb
Parent: Numeric

Infinity

A full featured Infinity class, supporting signed direction. Inifinty is a multiton based on direction The constant INFINITY is provided as the common case with direction=+1 (positive).

Besides being an class, these four constants are preset:

  NaN
  Inf
  PosInf
  NegInf

These four constants a built from two other basic constants:

  UNDEFINED
  INFINITY

In physical memory there are actually only three objects, namely three instances of InfinityClass, one for NaN, PosInf and NegInf, respectively.

Methods

+@   -@   <=>   ==   coerce   new   times   to_f   to_s  

Included Modules

Multiton

Classes and Modules

Class InfinityClass::Numeric

Constants

UNDEFINED = InfinityClass.new(0)   Set constant to positive infinity.
INFINITY = InfinityClass.new(1)
NaN = UNDEFINED
Inf = INFINITY
PosInf = +INFINITY
NegInf = -INFINITY

Attributes

direction  [R] 

Public Class methods

[Source]

# File lib/more/facets/infinity.rb, line 73
  def initialize(direction=1)
    @direction = (direction <=> 0)
    super()
  end

Public Instance methods

Noop.

[Source]

# File lib/more/facets/infinity.rb, line 90
  def +@
    self
  end

Change direction of infinity.

[Source]

# File lib/more/facets/infinity.rb, line 80
  def -@
    case @direction
    when  0 : self.class.instance(0)
    when  1 : self.class.instance(-1)
    when -1 : self.class.instance(1)
    end
  end

Comparision where infinity is alway greatest and negative infinityalways least.

[Source]

# File lib/more/facets/infinity.rb, line 136
  def <=>(other)
    case other
    when InfinityClass
      @direction <=> other.direction
    else
      @direction
    end
  end

Equality. Note that NaN != NaN.

[Source]

# File lib/more/facets/infinity.rb, line 120
  def ==(other)
    case other
    when InfinityClass
      if @direction == 0 and other.direction == 0
        false
      else
        super
      end
    else
      false
    end
  end

Coerce allows other numbers to be compared to infinity.

[Source]

# File lib/more/facets/infinity.rb, line 109
  def coerce(other)
    case other
    when InfinityClass
      super
    else
      return -self, other
    end
  end

Gee, a real infinite loop!

[Source]

# File lib/more/facets/infinity.rb, line 102
  def times
    loop do yield end
  end

  # Coerce allows other numbers to be
  # compared to infinity.

  def coerce(other)
    case other
    when InfinityClass
      super
    else
      return -self, other
    end
  end

  # Equality. Note that NaN != NaN.

  def ==(other)
    case other
    when InfinityClass
      if @direction == 0 and other.direction == 0
        false
      else
        super
      end
    else
      false
    end
  end

  # Comparision where infinity is alway greatest
  # and negative infinityalways least.

  def <=>(other)
    case other
    when InfinityClass
      @direction <=> other.direction
    else
      @direction
    end
  end

  #

  def to_s
    case @direction
    when  0  : "NaN"
    when  1  : "PosInf"
    when -1  : "NegInf"
    end
  end

end

Convert to the float version of infinity.

[Source]

# File lib/more/facets/infinity.rb, line 96
  def to_f
    (1.0/0) * @direction
  end

[Source]

# File lib/more/facets/infinity.rb, line 147
  def to_s
    case @direction
    when  0  : "NaN"
    when  1  : "PosInf"
    when -1  : "NegInf"
    end
  end

[Validate]