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:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if parameters are not numbers or compatible units



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

#tan(angle) ⇒ Numeric

Parameters:

Returns:



75
76
77
# File 'lib/ruby_units/math.rb', line 75

def tan(angle)
  angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to('radian').scalar) : super
end

#tanh(number) ⇒ Numeric

Parameters:

Returns:



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

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