Module: Teepee::CommanderMixins::Mathematics

Included in:
Teepee::Commander
Defined in:
lib/teepee/commander-mixins/mathematics.rb

Instance Method Summary collapse

Instance Method Details

#%(numbers) ⇒ Object


102
103
104
# File 'lib/teepee/commander-mixins/mathematics.rb', line 102

def % numbers
  to_numbers(numbers).inject { |base, percent| base*percent/100.0 }
end

#*(numbers) ⇒ Object


85
86
87
# File 'lib/teepee/commander-mixins/mathematics.rb', line 85

def * numbers
  ensure_numeric to_numbers(numbers).inject 1, :*
end

#**(numbers) ⇒ Object


98
99
100
# File 'lib/teepee/commander-mixins/mathematics.rb', line 98

def ** numbers
  ensure_numeric to_numbers(numbers).reduce :**
end

#+(numbers) ⇒ Object


72
73
74
# File 'lib/teepee/commander-mixins/mathematics.rb', line 72

def + numbers
  ensure_numeric to_numbers(numbers).inject 0, :+
end

#-(numbers) ⇒ Object


76
77
78
79
80
81
82
83
# File 'lib/teepee/commander-mixins/mathematics.rb', line 76

def - numbers
  numbers = to_numbers numbers
  if numbers.length == 1
    ensure_numeric -numbers.first
  else
    ensure_numeric numbers.reduce :-
  end
end

#/(numbers) ⇒ Object


89
90
91
92
93
94
95
96
# File 'lib/teepee/commander-mixins/mathematics.rb', line 89

def / numbers
  numbers = to_numbers numbers
  if numbers.length == 1
    ensure_numeric 1 / numbers.first
  else
    ensure_numeric numbers.reduce :/
  end
end

#acos(angle) ⇒ Object


118
119
120
# File 'lib/teepee/commander-mixins/mathematics.rb', line 118

def acos angle
  ensure_numeric Math.acos angle.to_number
end

#acosh(angle) ⇒ Object


122
123
124
# File 'lib/teepee/commander-mixins/mathematics.rb', line 122

def acosh angle
  ensure_numeric Math.acosh angle.to_number
end

#add_percentage(numbers) ⇒ Object


106
107
108
# File 'lib/teepee/commander-mixins/mathematics.rb', line 106

def add_percentage numbers
  to_numbers(numbers).inject {|base, percent| base * (1+percent/100.0) }
end

#asin(angle) ⇒ Object


126
127
128
# File 'lib/teepee/commander-mixins/mathematics.rb', line 126

def asin angle
  ensure_numeric Math.asin angle.to_number
end

#asinh(angle) ⇒ Object


130
131
132
# File 'lib/teepee/commander-mixins/mathematics.rb', line 130

def asinh angle
  ensure_numeric Math.asinh angle.to_number
end

#atan(angle) ⇒ Object


134
135
136
# File 'lib/teepee/commander-mixins/mathematics.rb', line 134

def atan angle
  ensure_numeric Math.atan angle.to_number
end

#atanh(angle) ⇒ Object


138
139
140
# File 'lib/teepee/commander-mixins/mathematics.rb', line 138

def atanh angle
  ensure_numeric Math.atanh angle.to_number
end

#ceiling(number) ⇒ Object


142
143
144
# File 'lib/teepee/commander-mixins/mathematics.rb', line 142

def ceiling number
  ensure_numeric number.to_number.ceil
end

#cos(angle) ⇒ Object


146
147
148
# File 'lib/teepee/commander-mixins/mathematics.rb', line 146

def cos angle
  ensure_numeric Math.cos angle.to_number
end

#cosh(angle) ⇒ Object


150
151
152
# File 'lib/teepee/commander-mixins/mathematics.rb', line 150

def cosh angle
  ensure_numeric Math.cosh angle.to_number
end

#degrees2radians(degrees) ⇒ Object


154
155
156
# File 'lib/teepee/commander-mixins/mathematics.rb', line 154

def degrees2radians degrees
  ensure_numeric degrees.to_number * Math::PI / 180.0
end

#eObject


158
159
160
# File 'lib/teepee/commander-mixins/mathematics.rb', line 158

def e
  Math::E
end

#ensure_numeric(number) ⇒ Object


50
51
52
53
54
55
56
57
58
# File 'lib/teepee/commander-mixins/mathematics.rb', line 50

def ensure_numeric number
  if number.kind_of? Complex
    command_error "Complex numbers are not yet supported."
  elsif not number.kind_of? Numeric
    command_error "Non-numeric result."
  else
    number
  end
end

#erf(number) ⇒ Object


162
163
164
# File 'lib/teepee/commander-mixins/mathematics.rb', line 162

def erf number
  ensure_numeric Math.erf number.to_number
end

#erfc(number) ⇒ Object


166
167
168
# File 'lib/teepee/commander-mixins/mathematics.rb', line 166

def erfc number
  ensure_numeric Math.erfc number.to_number
end

#floor(number) ⇒ Object


170
171
172
# File 'lib/teepee/commander-mixins/mathematics.rb', line 170

def floor number
  ensure_numeric number.to_number.floor
end

#gamma(number) ⇒ Object


174
175
176
# File 'lib/teepee/commander-mixins/mathematics.rb', line 174

def gamma number
  ensure_numeric Math.gamma number.to_number
end

#greater_than(numbers) ⇒ Object


178
179
180
181
182
183
184
185
186
# File 'lib/teepee/commander-mixins/mathematics.rb', line 178

def greater_than numbers
  if numbers.empty?
    true_constant
  elsif numbers.length == 1
    true_constant
  else
    numbers[0].to_number > numbers[1].to_number and greater_than numbers.rest
  end
end

#greater_than_or_equal(numbers) ⇒ Object


188
189
190
191
192
193
194
195
196
# File 'lib/teepee/commander-mixins/mathematics.rb', line 188

def greater_than_or_equal numbers
  if numbers.empty?
    true_constant
  elsif numbers.length == 1
    true_constant
  else
    numbers[0].to_number >= numbers[1].to_number and greater_than_or_equal numbers.rest
  end
end

#hypot(numbers) ⇒ Object


202
203
204
# File 'lib/teepee/commander-mixins/mathematics.rb', line 202

def hypot numbers
  ensure_numeric Math.sqrt to_numbers(numbers).map {|n| n**2}
end

#iObject


198
199
200
# File 'lib/teepee/commander-mixins/mathematics.rb', line 198

def i
  command_error "Complex numbers are not yet supported."
end

#ld(number) ⇒ Object


206
207
208
# File 'lib/teepee/commander-mixins/mathematics.rb', line 206

def ld number
  ensure_numeric Math.log2 number.to_number
end

#ldexp(fraction, exponent) ⇒ Object


210
211
212
# File 'lib/teepee/commander-mixins/mathematics.rb', line 210

def ldexp fraction, exponent
  ensure_numeric Math.ldexp fraction.to_number, exponent.to_number
end

#less_than(numbers) ⇒ Object


214
215
216
217
218
219
220
221
222
# File 'lib/teepee/commander-mixins/mathematics.rb', line 214

def less_than numbers
  if numbers.empty?
    true_constant
  elsif numbers.length == 1
    true_constant
  else
    numbers[0].to_number < numbers[1].to_number and less_than numbers.rest
  end
end

#less_than_or_equal(numbers) ⇒ Object


224
225
226
227
228
229
230
231
232
# File 'lib/teepee/commander-mixins/mathematics.rb', line 224

def less_than_or_equal numbers
  if numbers.empty?
    true_constant
  elsif numbers.length == 1
    true_constant
  else
    numbers[0].to_number <= numbers[1].to_number and less_than_or_equal numbers.rest
  end
end

#lgamma(number) ⇒ Object


234
235
236
# File 'lib/teepee/commander-mixins/mathematics.rb', line 234

def lgamma number
  ensure_numeric Math::lgamma(number.to_number).first
end

#ln(number) ⇒ Object


238
239
240
# File 'lib/teepee/commander-mixins/mathematics.rb', line 238

def ln number
  ensure_numeric Math.log number.to_number
end

#log(base, number) ⇒ Object


242
243
244
245
246
247
248
249
# File 'lib/teepee/commander-mixins/mathematics.rb', line 242

def log base, number
  if number.nil?
    number, base = base, number
    ensure_numeric Math.log10 number.to_number # default to log base 10
  else
    ensure_numeric Math.log number.to_number, base.to_number
  end
end

#log10(number) ⇒ Object


251
252
253
# File 'lib/teepee/commander-mixins/mathematics.rb', line 251

def log10 number
  ensure_numeric Math.log10 number.to_number
end

#mod(numbers) ⇒ Object


255
256
257
# File 'lib/teepee/commander-mixins/mathematics.rb', line 255

def mod numbers
  ensure_numeric to_numbers(numbers).reduce :%
end

#non_numeric_error(nan) ⇒ Object


46
47
48
# File 'lib/teepee/commander-mixins/mathematics.rb', line 46

def non_numeric_error nan
  command_error "The value \"#{nan}\" is not a number."
end

#number_from_word(word) ⇒ Object


60
61
62
63
64
65
66
# File 'lib/teepee/commander-mixins/mathematics.rb', line 60

def number_from_word word
  begin
    word.to_number
  rescue ArgumentError, NoMethodError
    nil
  end
end

#numeric?(*numbers) ⇒ Boolean

Returns:


42
43
44
# File 'lib/teepee/commander-mixins/mathematics.rb', line 42

def numeric? *numbers
  numbers.all? {|number| number.kind_of? Numeric}
end

#percent_total(numbers) ⇒ Object


114
115
116
# File 'lib/teepee/commander-mixins/mathematics.rb', line 114

def percent_total numbers
  to_numbers(numbers).inject {|total, part| Float(part)/Float(total)*100.0 }
end

#piObject


259
260
261
# File 'lib/teepee/commander-mixins/mathematics.rb', line 259

def pi
  Math::PI
end

#radians2degrees(radians) ⇒ Object


263
264
265
# File 'lib/teepee/commander-mixins/mathematics.rb', line 263

def radians2degrees radians
  ensure_numeric(radians.to_number * 180.0 / Math::PI)
end

#round(number, precision = nil, *_) ⇒ Object


267
268
269
270
271
272
273
# File 'lib/teepee/commander-mixins/mathematics.rb', line 267

def round number, precision = nil, *_
  if precision.nil? or precision.to_number.nil?
    ensure_numeric number.to_number.round
  else
    ensure_numeric number.to_number.round precision.to_number
  end
end

#sin(angle) ⇒ Object


275
276
277
# File 'lib/teepee/commander-mixins/mathematics.rb', line 275

def sin angle
  ensure_numeric Math.sin angle.to_number
end

#sinh(angle) ⇒ Object


279
280
281
# File 'lib/teepee/commander-mixins/mathematics.rb', line 279

def sinh angle
  ensure_numeric Math.sinh angle.to_number
end

#sqrt(number) ⇒ Object


283
284
285
# File 'lib/teepee/commander-mixins/mathematics.rb', line 283

def sqrt number
  ensure_numeric Math.sqrt number.to_number
end

#subtract_percentage(numbers) ⇒ Object


110
111
112
# File 'lib/teepee/commander-mixins/mathematics.rb', line 110

def subtract_percentage numbers
  to_numbers(numbers).inject {|base, percent| base * (1-percent/100.0) }
end

#tan(angle) ⇒ Object


287
288
289
# File 'lib/teepee/commander-mixins/mathematics.rb', line 287

def tan angle
  ensure_numeric Math.tan angle.to_number
end

#tanh(angle) ⇒ Object


291
292
293
# File 'lib/teepee/commander-mixins/mathematics.rb', line 291

def tanh angle
  ensure_numeric Math.tanh angle.to_number
end

#to_numbers(words) ⇒ Object


68
69
70
# File 'lib/teepee/commander-mixins/mathematics.rb', line 68

def to_numbers words
  words.map {|word| number_from_word word}.reject &:nil?
end