Module: VectorNumber::Comparing
Overview
Methods for comparing with other numbers.
Comparable is included for parity with numbers.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compare to
otherand return -1, 0, or 1 ifselfis less than, equal, or larger thanother. -
#==(other) ⇒ Boolean
Compare to
otherfor equality. -
#eql?(other) ⇒ Boolean
Compare to
otherfor strict equality.
Instance Method Details
#<=>(other) ⇒ Integer?
Compare to other and return -1, 0, or 1 if self is less than, equal, or larger than other.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/vector_number/comparing.rb', line 95 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
-
otheris a VectorNumber and it iseql?to this one, or -
otheris a Numeric equal in value to this (real or complex) number.
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.
Note that VectorNumber#options are not considered for equality.
62 63 64 65 66 67 68 69 70 |
# File 'lib/vector_number/comparing.rb', line 62 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 |