Class: Rubic::Builtin::Number

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/rubic/builtin/number.rb

Instance Method Summary collapse

Methods included from Util

#exact_to_inexact, #inexact_to_exact, #normalize_number

Instance Method Details

#*(*args) ⇒ Object



24
25
26
# File 'lib/rubic/builtin/number.rb', line 24

def *(*args)
  transitive_operation('*', 1, args) {|a, b| a * b }
end

#+(*args) ⇒ Object



9
10
11
# File 'lib/rubic/builtin/number.rb', line 9

def +(*args)
  transitive_operation('+', 0, args) {|a, b| a + b }
end

#-(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/rubic/builtin/number.rb', line 13

def -(*args)
  case args.size
  when 0
    raise Rubic::ArgumentError, "wrong number of arguments (0 for 1+)"
  when 1
    -args.first
  else
    transitive_operation('-', args) {|a, b| a - b }
  end
end

#/(*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/rubic/builtin/number.rb', line 28

def /(*args)
  case args.size
  when 0
    raise Rubic::ArgumentError, "wrong number of arguments (0 for 1+)"
  when 1
    1.quo(args.first)
  else
    transitive_operation('/', args) {|a, b| a.quo(b) }
  end
end

#<(*args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/rubic/builtin/number.rb', line 61

def <(*args)
  if args.size < 2
    raise Rubic::ArgumentError, "wrong number of arguments (#{args.size} for 2+)"
  else
    transitive_operation('<', args) do |a, b|
      (a < b) ? b : (return false)
    end
    true
  end
end

#<=(*args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/rubic/builtin/number.rb', line 83

def <=(*args)
  if args.size < 2
    raise Rubic::ArgumentError, "wrong number of arguments (#{args.size} for 2+)"
  else
    transitive_operation('<=', args) do |a, b|
      (a <= b) ? b : (return false)
    end
    true
  end
end

#>(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/rubic/builtin/number.rb', line 50

def >(*args)
  if args.size < 2
    raise Rubic::ArgumentError, "wrong number of arguments (#{args.size} for 2+)"
  else
    transitive_operation('>', args) do |a, b|
      (a > b) ? b : (return false)
    end
    true
  end
end

#>=(*args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/rubic/builtin/number.rb', line 72

def >=(*args)
  if args.size < 2
    raise Rubic::ArgumentError, "wrong number of arguments (#{args.size} for 2+)"
  else
    transitive_operation('>=', args) do |a, b|
      (a >= b) ? b : (return false)
    end
    true
  end
end

#abs(num) ⇒ Object



180
181
182
183
# File 'lib/rubic/builtin/number.rb', line 180

def abs(num)
  ensure_number num
  num.abs
end

#acos(num) ⇒ Object



290
291
292
293
# File 'lib/rubic/builtin/number.rb', line 290

def acos(num)
  ensure_number num
  CMath.acos(num)
end

#angle(num) ⇒ Object



343
344
345
346
# File 'lib/rubic/builtin/number.rb', line 343

def angle(num)
  ensure_number num
  num.angle
end

#asin(num) ⇒ Object



285
286
287
288
# File 'lib/rubic/builtin/number.rb', line 285

def asin(num)
  ensure_number num
  CMath.asin(num)
end

#atan(*args) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/rubic/builtin/number.rb', line 295

def atan(*args)
  case args.size
  when 1
    ensure_number args[0]
    CMath.atan(args[0])
  when 2
    ensure_number args[0], args[1]
    Complex(args[1], args[0]).angle
  else
    raise Rubic::ArgumentError, "wrong number of arguments (#{args.size} for 1..2)"
  end
end

#ceiling(num) ⇒ Object



240
241
242
243
# File 'lib/rubic/builtin/number.rb', line 240

def ceiling(num)
  ensure_real num
  num.ceil
end

#complex?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/rubic/builtin/number.rb', line 98

def complex?(suspect)
  suspect.is_a?(Complex) || real?(suspect)
end

#cos(num) ⇒ Object



275
276
277
278
# File 'lib/rubic/builtin/number.rb', line 275

def cos(num)
  ensure_number num
  CMath.cos(num)
end

#denominator(num) ⇒ Object



229
230
231
232
233
# File 'lib/rubic/builtin/number.rb', line 229

def denominator(num)
  ensure_real num
  ret = num.to_r.denominator
  exact?(num) ? ret : exact_to_inexact(ret)
end

#even?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
# File 'lib/rubic/builtin/number.rb', line 157

def even?(suspect)
  ensure_integer suspect
  suspect.to_i.even?
end

#exact?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubic/builtin/number.rb', line 118

def exact?(suspect)
  case suspect
  when Complex
    exact?(suspect.real) && exact?(suspect.imag)
  when Float
    false
  when Rational
    true
  when Integer
    true
  else
    false
  end
end

#exp(num) ⇒ Object



260
261
262
263
# File 'lib/rubic/builtin/number.rb', line 260

def exp(num)
  ensure_number num
  CMath.exp(num)
end

#expt(x, y) ⇒ Object



313
314
315
316
# File 'lib/rubic/builtin/number.rb', line 313

def expt(x, y)
  ensure_number x, y
  x ** y
end

#floor(num) ⇒ Object



235
236
237
238
# File 'lib/rubic/builtin/number.rb', line 235

def floor(num)
  ensure_real num
  num.floor
end

#gcd(*args) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/rubic/builtin/number.rb', line 203

def gcd(*args)
  exact = true
  ret = args.reduce(0) do |res, num|
    ensure_integer num
    exact &&= exact?(num)
    res.gcd(num.to_i)
  end
  exact ? ret : exact_to_inexact(ret)
end

#inexact?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/rubic/builtin/number.rb', line 133

def inexact?(suspect)
  number?(suspect) && !exact?(suspect)
end

#integer?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
# File 'lib/rubic/builtin/number.rb', line 110

def integer?(suspect)
  if suspect.is_a?(Float)
    suspect == suspect.truncate # X.0
  else
    suspect.is_a?(Integer)
  end
end

#lcm(*args) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/rubic/builtin/number.rb', line 213

def lcm(*args)
  exact = true
  ret = args.reduce(1) do |res, num|
    ensure_integer num
    exact &&= exact?(num)
    res.lcm(num.to_i)
  end
  exact ? ret : exact_to_inexact(ret)
end

#log(num) ⇒ Object



265
266
267
268
# File 'lib/rubic/builtin/number.rb', line 265

def log(num)
  ensure_number num
  CMath.log(num)
end

#magnitude(num) ⇒ Object



338
339
340
341
# File 'lib/rubic/builtin/number.rb', line 338

def magnitude(num)
  ensure_number num
  num.magnitude
end

#max(a, *args) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/rubic/builtin/number.rb', line 162

def max(a, *args)
  args.unshift(a)
  exact = args.all? do |e|
    ensure_real e
    exact?(e)
  end
  exact ? args.max : exact_to_inexact(args.max)
end

#min(a, *args) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/rubic/builtin/number.rb', line 171

def min(a, *args)
  args.unshift(a)
  exact = args.all? do |e|
    ensure_real e
    exact?(e)
  end
  exact ? args.min : exact_to_inexact(args.min)
end

#modulo(a, b) ⇒ Object



191
192
193
194
195
# File 'lib/rubic/builtin/number.rb', line 191

def modulo(a, b)
  ensure_integer a, b
  ret = a.modulo(b)
  exact?(a) && exact?(b) ? ret : exact_to_inexact(ret)
end

#negative?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/rubic/builtin/number.rb', line 147

def negative?(suspect)
  ensure_real suspect
  suspect < 0.0
end

#number?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/rubic/builtin/number.rb', line 94

def number?(suspect)
  suspect.is_a?(Numeric) || complex?(suspect)
end

#numerator(num) ⇒ Object



223
224
225
226
227
# File 'lib/rubic/builtin/number.rb', line 223

def numerator(num)
  ensure_real num
  ret = num.to_r.numerator
  exact?(num) ? ret : exact_to_inexact(ret)
end

#odd?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
# File 'lib/rubic/builtin/number.rb', line 152

def odd?(suspect)
  ensure_integer suspect
  suspect.to_i.odd?
end

#positive?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
# File 'lib/rubic/builtin/number.rb', line 142

def positive?(suspect)
  ensure_real suspect
  suspect > 0.0
end

#quotient(a, b) ⇒ Object



185
186
187
188
189
# File 'lib/rubic/builtin/number.rb', line 185

def quotient(a, b)
  ensure_integer a, b
  ret = a.quo(b).round
  exact?(a) && exact?(b) ? ret : exact_to_inexact(ret)
end

#rational?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rubic/builtin/number.rb', line 106

def rational?(suspect)
  suspect.is_a?(Rational) || integer?(suspect)
end

#rationalize(num, eps) ⇒ Object



255
256
257
258
# File 'lib/rubic/builtin/number.rb', line 255

def rationalize(num, eps)
  ensure_real num
  normalize_number num.rationalize(eps)
end

#real?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/rubic/builtin/number.rb', line 102

def real?(suspect)
  suspect.is_a?(Float) || rational?(suspect)
end

#remainder(a, b) ⇒ Object



197
198
199
200
201
# File 'lib/rubic/builtin/number.rb', line 197

def remainder(a, b)
  ensure_integer a, b
  ret = a.remainder(b)
  exact?(a) && exact?(b) ? ret : exact_to_inexact(ret)
end

#round(num) ⇒ Object



250
251
252
253
# File 'lib/rubic/builtin/number.rb', line 250

def round(num)
  ensure_real num
  num.round
end

#sin(num) ⇒ Object



270
271
272
273
# File 'lib/rubic/builtin/number.rb', line 270

def sin(num)
  ensure_number num
  CMath.sin(num)
end

#sqrt(num) ⇒ Object



308
309
310
311
# File 'lib/rubic/builtin/number.rb', line 308

def sqrt(num)
  ensure_number num
  CMath.sqrt(num)
end

#tan(num) ⇒ Object



280
281
282
283
# File 'lib/rubic/builtin/number.rb', line 280

def tan(num)
  ensure_number num
  CMath.tan(num)
end

#truncate(num) ⇒ Object



245
246
247
248
# File 'lib/rubic/builtin/number.rb', line 245

def truncate(num)
  ensure_real num
  num.truncate
end

#zero?(suspect) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/rubic/builtin/number.rb', line 137

def zero?(suspect)
  ensure_number suspect
  suspect.zero?
end