Module: VectorNumber::Comparing

Includes:
Comparable
Included in:
VectorNumber
Defined in:
lib/vector_number/comparing.rb

Overview

Methods for comparing with other numbers.

Comparable is included for parity with numbers.

Examples:

using Comparable methods

VectorNumber[12] < 0 # => false
VectorNumber[12, "a"] < 0 # ArgumentError
(VectorNumber[12]..VectorNumber[15]).include?(13) # => true

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Integer?

Compare to other and return -1, 0, or 1 if self is less than, equal, or larger than other.

Examples:

VectorNumber[130] <=> 12 # => 1
1 <=> VectorNumber[13] # => -1
VectorNumber[12.1] <=> Complex(12.1, 0) # => 0
# This doesn't work as expected without NumericRefinements:
Complex(12.1, 0) <=> VectorNumber[12.1] # => nil

# Any non-real comparison returns nil:
VectorNumber[12.1] <=> Complex(12.1, 1) # => nil
VectorNumber[12.1i] <=> 2 # => nil
VectorNumber["a"] <=> 2 # => nil

Parameters:

  • other (Object)

Returns:

  • (Integer)
  • (nil)

    if self or other isn’t a real number.

See Also:

Since:

  • 0.2.0



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vector_number/comparing.rb', line 112

def <=>(other)
  return nil unless numeric?(1)

  case other
  when VectorNumber
    other.numeric?(1) ? real <=> other.real : nil
  when Numeric
    other.imaginary.zero? ? real <=> other.real : nil
  else
    nil
  end
end

#==(other) ⇒ Boolean

Compare to other for equality.

Values are considered equal if

  • other is a VectorNumber and it is eql? to this one, or

  • other is a Numeric equal in value to this (real or complex) number.

Examples:

VectorNumber[3.13] == 3.13 # => true
VectorNumber[1.4, 1.5i] == Complex(1.4, 1.5) # => true
VectorNumber["a", "b", "c"] == VectorNumber["c", "b", "a"] # => true
VectorNumber["a", 14] == 14 # => false
VectorNumber["a"] == "a" # => false

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 0.2.0



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vector_number/comparing.rb', line 32

def ==(other)
  return true if eql?(other)

  case other
  when Numeric
    numeric?(2) && other.real == real && other.imaginary == imaginary
  else
    # Can't compare a number-like value to a non-number.
    false
  end
end

#eql?(other) ⇒ Boolean

Compare to other for strict equality.

Values are considered equal only if other is a VectorNumber and it has exactly the same units and coefficients, though possibly in a different order. Additionally, ‘a.eql?(b)` implies `a.hash == b.hash`.

Note that VectorNumber#options are not considered for equality.

Examples:

VectorNumber["a", "b", "c"].eql? VectorNumber["c", "b", "a"] # => true
VectorNumber[3.13].eql? 3.13 # => false
VectorNumber[1.4, 1.5i].eql? Complex(1.4, 1.5) # => false
VectorNumber["a", 14].eql? 14 # => false
VectorNumber["a"].eql? "a" # => false

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 0.1.0



63
64
65
66
67
68
69
70
71
# File 'lib/vector_number/comparing.rb', line 63

def eql?(other)
  return true if equal?(other)

  if other.is_a?(VectorNumber)
    other.size == size && other.each_pair.all? { |u, c| @data[u] == c }
  else
    false
  end
end

#hashInteger

Generate an Integer hash value for self.

Hash values are stable during runtime, but not between processes.

Examples:

VectorNumber["b", "a"].hash # => 3081872088394655324
VectorNumber["a", "b", mult: :cross].hash # => 3081872088394655324
VectorNumber["b", "c"].hash # => -1002381358514682371

Returns:

  • (Integer)

Since:

  • 0.4.2



85
86
87
# File 'lib/vector_number/comparing.rb', line 85

def hash
  [self.class, @data].hash
end