module RubyUnits::Math
Math
will convert unit objects to radians and then attempt to use the value for trigonometric functions.
Public Instance Methods
@param number [Numeric, RubyUnits::Unit
] @return [Numeric, RubyUnits::Unit
]
# 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
@param number [Numeric, RubyUnits::Unit
] @return [Numeric, RubyUnits::Unit
]
# 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
@param number [Numeric, RubyUnits::Unit
] @return [Numeric] if argument is a number @return [RubyUnits::Unit] if argument is a unit
# 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
@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
# 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
Take the cube root of a unit or number
@param number [Numeric, RubyUnits::Unit
] @return [Numeric, RubyUnits::Unit
]
# 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
@param angle [Numeric, RubyUnits::Unit
] @return [Numeric]
# File lib/ruby_units/math.rb, line 49 def cos(angle) angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super end
@param number [Numeric, RubyUnits::Unit
] @return [Numeric]
# File lib/ruby_units/math.rb, line 71 def cosh(number) number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super end
@param x [Numeric, RubyUnits::Unit
] @param y [Numeric, RubyUnits::Unit
] @return [Numeric]
# 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
@param number [Numeric, RubyUnits::Unit
] @param base [Numeric] @return [Numeric]
# 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
@param number [Numeric, RubyUnits::Unit
] @return [Numeric]
# 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
@param angle [Numeric, RubyUnits::Unit
] @return [Numeric]
# File lib/ruby_units/math.rb, line 33 def sin(angle) angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super end
@param number [Numeric, RubyUnits::Unit
] @return [Numeric]
# File lib/ruby_units/math.rb, line 65 def sinh(number) number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super end
Take the square root of a unit or number
@param number [Numeric, RubyUnits::Unit
] @return [Numeric, RubyUnits::Unit
]
# 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
@param angle [Numeric, RubyUnits::Unit
] @return [Numeric]
# File lib/ruby_units/math.rb, line 77 def tan(angle) angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to("radian").scalar) : super end
@param number [Numeric, RubyUnits::Unit
] @return [Numeric]
# File lib/ruby_units/math.rb, line 83 def tanh(number) number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super end