Module: RubyUnits::Math
- Included in:
- Math
- Defined in:
- lib/ruby_units/math.rb
Overview
Math will convert unit objects to radians and then attempt to use the value for trigonometric functions.
Instance Method Summary collapse
- #acos(number) ⇒ Numeric, RubyUnits::Unit
- #asin(number) ⇒ Numeric, RubyUnits::Unit
- #atan(number) ⇒ Numeric, RubyUnits::Unit
- #atan2(x, y) ⇒ Numeric, RubyUnits::Unit
-
#cbrt(number) ⇒ Numeric, RubyUnits::Unit
Take the cube root of a unit or number.
- #cos(angle) ⇒ Numeric
- #cosh(number) ⇒ Numeric
- #hypot(x, y) ⇒ Numeric
- #log(number, base = ::Math::E) ⇒ Numeric
- #log10(number) ⇒ Numeric
- #sin(angle) ⇒ Numeric
- #sinh(number) ⇒ Numeric
-
#sqrt(number) ⇒ Numeric, RubyUnits::Unit
Take the square root of a unit or number.
- #tan(angle) ⇒ Numeric
- #tanh(number) ⇒ Numeric
Instance Method Details
#acos(number) ⇒ Numeric, RubyUnits::Unit
53 54 55 56 57 58 59 |
# File 'lib/ruby_units/math.rb', line 53 def acos(number) if number.is_a?(RubyUnits::Unit) [super(number), 'radian'].to_unit else super end end |
#asin(number) ⇒ Numeric, RubyUnits::Unit
37 38 39 40 41 42 43 |
# File 'lib/ruby_units/math.rb', line 37 def asin(number) if number.is_a?(RubyUnits::Unit) [super(number), 'radian'].to_unit else super end end |
#atan(number) ⇒ Numeric, RubyUnits::Unit
99 100 101 102 103 104 105 |
# File 'lib/ruby_units/math.rb', line 99 def atan(number) if number.is_a?(RubyUnits::Unit) [super(number), 'radian'].to_unit else super end end |
#atan2(x, y) ⇒ Numeric, RubyUnits::Unit
112 113 114 115 116 117 118 119 120 |
# File 'lib/ruby_units/math.rb', line 112 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) ⇒ Numeric, RubyUnits::Unit
Take the cube root of a unit or number
21 22 23 24 25 26 27 |
# File 'lib/ruby_units/math.rb', line 21 def cbrt(number) if number.is_a?(RubyUnits::Unit) (number**Rational(1, 3)).to_unit else super end end |
#cos(angle) ⇒ Numeric
47 48 49 |
# File 'lib/ruby_units/math.rb', line 47 def cos(angle) angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to('radian').scalar) : super end |
#cosh(number) ⇒ Numeric
69 70 71 |
# File 'lib/ruby_units/math.rb', line 69 def cosh(number) number.is_a?(RubyUnits::Unit) ? super(number.convert_to('radian').scalar) : super end |
#hypot(x, y) ⇒ Numeric
88 89 90 91 92 93 94 |
# File 'lib/ruby_units/math.rb', line 88 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) ⇒ Numeric
135 136 137 138 139 140 141 |
# File 'lib/ruby_units/math.rb', line 135 def log(number, base = ::Math::E) if number.is_a?(RubyUnits::Unit) super(number.to_f, base) else super end end |
#log10(number) ⇒ Numeric
124 125 126 127 128 129 130 |
# File 'lib/ruby_units/math.rb', line 124 def log10(number) if number.is_a?(RubyUnits::Unit) super(number.to_f) else super end end |
#sin(angle) ⇒ Numeric
31 32 33 |
# File 'lib/ruby_units/math.rb', line 31 def sin(angle) angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to('radian').scalar) : super end |
#sinh(number) ⇒ Numeric
63 64 65 |
# File 'lib/ruby_units/math.rb', line 63 def sinh(number) number.is_a?(RubyUnits::Unit) ? super(number.convert_to('radian').scalar) : super end |
#sqrt(number) ⇒ Numeric, RubyUnits::Unit
Take the square root of a unit or number
9 10 11 12 13 14 15 |
# File 'lib/ruby_units/math.rb', line 9 def sqrt(number) if number.is_a?(RubyUnits::Unit) (number**Rational(1, 2)).to_unit else super end end |