module RubyUnits::Math

Math will convert unit objects to radians and then attempt to use the value for trigonometric functions.

Public Instance Methods

acos(number) click to toggle source

@param number [Numeric, RubyUnits::Unit] @return [Numeric, RubyUnits::Unit]

Calls superclass method
# File lib/ruby_units/math.rb, line 55
def acos(number)
  if number.is_a?(RubyUnits::Unit)
    [super, "radian"].to_unit
  else
    super
  end
end
asin(number) click to toggle source

@param number [Numeric, RubyUnits::Unit] @return [Numeric, RubyUnits::Unit]

Calls superclass method
# File lib/ruby_units/math.rb, line 39
def asin(number)
  if number.is_a?(RubyUnits::Unit)
    [super, "radian"].to_unit
  else
    super
  end
end
atan(number) click to toggle source

@param number [Numeric, RubyUnits::Unit] @return [Numeric] if argument is a number @return [RubyUnits::Unit] if argument is a unit

Calls superclass method
# File lib/ruby_units/math.rb, line 101
def atan(number)
  if number.is_a?(RubyUnits::Unit)
    [super, "radian"].to_unit
  else
    super
  end
end
atan2(x, y) click to toggle source

@param x [Numeric, RubyUnits::Unit] @param y [Numeric, RubyUnits::Unit] @return [Numeric] if all parameters are numbers @return [RubyUnits::Unit] if parameters are units @raise [ArgumentError] if parameters are not numbers or compatible units

Calls superclass method
# File lib/ruby_units/math.rb, line 114
def atan2(x, y)
  raise ArgumentError, "Incompatible RubyUnits::Units" if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && !x.compatible?(y)

  if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && x.compatible?(y)
    [super(x.base_scalar, y.base_scalar), "radian"].to_unit
  else
    super
  end
end
cbrt(number) click to toggle source

Take the cube root of a unit or number

@param number [Numeric, RubyUnits::Unit] @return [Numeric, RubyUnits::Unit]

Calls superclass method
# File lib/ruby_units/math.rb, line 23
def cbrt(number)
  if number.is_a?(RubyUnits::Unit)
    (number**Rational(1, 3)).to_unit
  else
    super
  end
end
cos(angle) click to toggle source

@param angle [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 49
def cos(angle)
  angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super
end
cosh(number) click to toggle source

@param number [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 71
def cosh(number)
  number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super
end
hypot(x, y) click to toggle source

@param x [Numeric, RubyUnits::Unit] @param y [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 90
def hypot(x, y)
  if x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)
    ((x**2) + (y**2))**Rational(1, 2)
  else
    super
  end
end
log(number, base = ::Math::E) click to toggle source

@param number [Numeric, RubyUnits::Unit] @param base [Numeric] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 137
def log(number, base = ::Math::E)
  if number.is_a?(RubyUnits::Unit)
    super(number.to_f, base)
  else
    super
  end
end
log10(number) click to toggle source

@param number [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 126
def log10(number)
  if number.is_a?(RubyUnits::Unit)
    super(number.to_f)
  else
    super
  end
end
sin(angle) click to toggle source

@param angle [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 33
def sin(angle)
  angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super
end
sinh(number) click to toggle source

@param number [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 65
def sinh(number)
  number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super
end
sqrt(number) click to toggle source

Take the square root of a unit or number

@param number [Numeric, RubyUnits::Unit] @return [Numeric, RubyUnits::Unit]

Calls superclass method
# File lib/ruby_units/math.rb, line 11
def sqrt(number)
  if number.is_a?(RubyUnits::Unit)
    (number**Rational(1, 2)).to_unit
  else
    super
  end
end
tan(angle) click to toggle source

@param angle [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 77
def tan(angle)
  angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super
end
tanh(number) click to toggle source

@param number [Numeric, RubyUnits::Unit] @return [Numeric]

Calls superclass method
# File lib/ruby_units/math.rb, line 83
def tanh(number)
  number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super
end