Module: 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.

Class Method Summary collapse

Class Method Details

.atan2(x, y) ⇒ Numeric

Returns:



116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby_units/math.rb', line 116

def atan2(x,y)
  case
  when (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && (x !~ y)
    raise ArgumentError, "Incompatible RubyUnits::Units"
  when (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && (x =~ y)
    Math::unit_atan2(x.base_scalar, y.base_scalar)
  else
    Math::unit_atan2(x,y)
  end
end

.cbrt(n) ⇒ Numeric

Returns:



25
26
27
28
29
30
31
# File 'lib/ruby_units/math.rb', line 25

def cbrt(n)
  if RubyUnits::Unit === n
    (n**(Rational(1,3))).to_unit 
  else
    unit_cbrt(n)
  end
end

.cos(n) ⇒ Numeric

Returns:



51
52
53
# File 'lib/ruby_units/math.rb', line 51

def cos(n)
  RubyUnits::Unit === n ? unit_cos(n.convert_to('radian').scalar) : unit_cos(n)
end

.cosh(n) ⇒ Numeric

Returns:



71
72
73
# File 'lib/ruby_units/math.rb', line 71

def cosh(n)
  RubyUnits::Unit === n ? unit_cosh(n.convert_to('radian').scalar) : unit_cosh(n)
end

.hypot(x, y) ⇒ Numeric

Convert parameters to consistent units and perform the function

Returns:



102
103
104
105
106
107
108
# File 'lib/ruby_units/math.rb', line 102

def hypot(x,y)
  if RubyUnits::Unit === x && RubyUnits::Unit === y
    (x**2 + y**2)**(1/2)
  else
    unit_hypot(x,y)
  end
end

.sin(n) ⇒ Numeric

Returns:



41
42
43
# File 'lib/ruby_units/math.rb', line 41

def sin(n)
 RubyUnits::Unit === n ? unit_sin(n.convert_to('radian').scalar) : unit_sin(n)
end

.sinh(n) ⇒ Numeric

Returns:



61
62
63
# File 'lib/ruby_units/math.rb', line 61

def sinh(n)
  RubyUnits::Unit === n ? unit_sinh(n.convert_to('radian').scalar) : unit_sinh(n)
end

.sqrt(n) ⇒ Numeric

Returns:



9
10
11
12
13
14
15
# File 'lib/ruby_units/math.rb', line 9

def sqrt(n)
  if RubyUnits::Unit === n
    (n**(Rational(1,2))).to_unit 
  else
    unit_sqrt(n)
  end
end

.tan(n) ⇒ Numeric

Returns:



81
82
83
# File 'lib/ruby_units/math.rb', line 81

def tan(n)
 RubyUnits::Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
end

.tanh(n) ⇒ Numeric

Returns:



91
92
93
# File 'lib/ruby_units/math.rb', line 91

def tanh(n)
  RubyUnits::Unit === n ? unit_tanh(n.convert_to('radian').scalar) : unit_tanh(n)
end

.unit_atan2Object



114
# File 'lib/ruby_units/math.rb', line 114

alias :unit_atan2 :atan2

.unit_cbrtObject



23
# File 'lib/ruby_units/math.rb', line 23

alias :unit_cbrt :cbrt

.unit_cosObject



49
# File 'lib/ruby_units/math.rb', line 49

alias :unit_cos :cos

.unit_coshObject



69
# File 'lib/ruby_units/math.rb', line 69

alias :unit_cosh :cosh

.unit_hypotObject



99
# File 'lib/ruby_units/math.rb', line 99

alias :unit_hypot :hypot

.unit_sinObject

:nocov:



39
# File 'lib/ruby_units/math.rb', line 39

alias :unit_sin :sin

.unit_sinhObject



59
# File 'lib/ruby_units/math.rb', line 59

alias :unit_sinh :sinh

.unit_sqrtObject



7
# File 'lib/ruby_units/math.rb', line 7

alias :unit_sqrt :sqrt

.unit_tanObject



79
# File 'lib/ruby_units/math.rb', line 79

alias :unit_tan :tan

.unit_tanhObject



89
# File 'lib/ruby_units/math.rb', line 89

alias :unit_tanh :tanh