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

Instance Method Details

#acos(number) ⇒ Numeric, RubyUnits::Unit

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if parameters are not numbers or compatible units



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

#tan(angle) ⇒ Numeric

Parameters:

Returns:



77
78
79
# 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) ⇒ Numeric

Parameters:

Returns:



83
84
85
# File 'lib/ruby_units/math.rb', line 83

def tanh(number)
  number.is_a?(RubyUnits::Unit) ? super(number.convert_to("radian").scalar) : super
end