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
55 56 57 58 59 60 61 |
# 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) ⇒ Numeric, RubyUnits::Unit
39 40 41 42 43 44 45 |
# 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) ⇒ Numeric, RubyUnits::Unit
101 102 103 104 105 106 107 |
# 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) ⇒ Numeric, RubyUnits::Unit
114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Numeric, RubyUnits::Unit
Take the cube root of a unit or number
23 24 25 26 27 28 29 |
# 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) ⇒ Numeric
49 50 51 |
# 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) ⇒ Numeric
71 72 73 |
# 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) ⇒ Numeric
90 91 92 93 94 95 96 |
# 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) ⇒ Numeric
137 138 139 140 141 142 143 |
# 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) ⇒ Numeric
126 127 128 129 130 131 132 |
# 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) ⇒ Numeric
33 34 35 |
# 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) ⇒ Numeric
65 66 67 |
# 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) ⇒ Numeric, RubyUnits::Unit
Take the square root of a unit or number
11 12 13 14 15 16 17 |
# 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 |