module SY::SignedMagnitude

Qualities specific to relative magnitudes (mixin).

Public Class Methods

new( of: nil, amount: nil ) click to toggle source

Relative magnitude constructor takes :quantity (alias :of) argument and :amount argument. Amount is allowed to be negative.

# File lib/sy/signed_magnitude.rb, line 9
def initialize( of: nil, amount: nil )
  fail ArgumentError, "Quantity (:of) argument missing!" if of.nil?
  @quantity = of
  @amount = case amount
            when Numeric then amount
            when nil then 1
            else
              begin
                amount.( @quantity ).amount
              rescue NameError, NoMethodError; amount end
            end
end

Public Instance Methods

+(m2) click to toggle source

Addition.

# File lib/sy/signed_magnitude.rb, line 24
def + m2
  return magnitude( amount + m2.amount ) if quantity == m2.quantity
  return quantity.absolute.magnitude( amount + m2.amount ) if
    quantity.absolute == m2.quantity
  compat_1, compat_2 = m2.coerce self
  return compat_1 + compat_2
end
-(m2) click to toggle source

Subtraction.

# File lib/sy/signed_magnitude.rb, line 34
def - m2
  return magnitude( amount - m2.amount ) if m2.quantity == quantity.relative
  return quantity.relative.magnitude( amount - m2.amount ) if
    quantity == m2.quantity
  compat_1, compat_2 = m2.coerce self
  return compat_1 - compat_2
end

Private Instance Methods

çς() click to toggle source

String describing this class.

# File lib/sy/signed_magnitude.rb, line 46
def çς
  "±Magnitude"
end