rubysl-rational 1.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d97dd5bf85e7b762cfdc4c32a3b49a7eafad136
4
- data.tar.gz: b3f2880fd75ab9be870b6734c1a215f66bb62dfc
3
+ metadata.gz: b210f85f8fbe4b7df649c2c3beec1742055d0e41
4
+ data.tar.gz: 11ac42a7a2c801283b3ec929684d778ea1ba559a
5
5
  SHA512:
6
- metadata.gz: 565531733759d1ec62f50bcf53131c863ebcb6ccce6c60d88829b0e9b2f56e10ed3c42057268f1db7e2665ee72b6f4b1cf55101fd1acfe975f440f6e2ee6939b
7
- data.tar.gz: cfd2afa8e6d0f4e4a6032903a8ffc6edf948bcf35e94f58b12b3fda81e8cd7492cde2f8cdcc36a25a0192d2b038f88a029bceae8d1b74a41fbc86efcb06e01c8
6
+ metadata.gz: d942df924f852a76dbab1c7f951bfa179f1a0e892ba8d58f0558322d538752ea94fd7f02e6741003b8dcfa2f5d84256fddae797c9c21c6306651b5f09951bab9
7
+ data.tar.gz: 33195335b008d0a9f20bfc35571174838f4d908f53e69c0a7f76431d09b57ee959df1e68bb5ce664d521b2a3d9930199673c62fe310192b0fc3c34e7d5b081b2
@@ -1,7 +1,6 @@
1
1
  language: ruby
2
2
  env:
3
3
  - RUBYLIB=lib
4
- script: bundle exec mspec
4
+ script: bundle exec mspec spec
5
5
  rvm:
6
- - 1.8.7
7
- - rbx-nightly-18mode
6
+ - rbx-nightly-19mode
@@ -1,562 +1,31 @@
1
- #
2
- # rational.rb -
3
- # $Release Version: 0.5 $
4
- # $Revision: 1.7 $
5
- # $Date: 1999/08/24 12:49:28 $
6
- # by Keiju ISHITSUKA(SHL Japan Inc.)
7
- #
8
- # Documentation by Kevin Jackson and Gavin Sinclair.
9
- #
10
- # When you <tt>require 'rational'</tt>, all interactions between numbers
11
- # potentially return a rational result. For example:
12
- #
13
- # 1.quo(2) # -> 0.5
14
- # require 'rational'
15
- # 1.quo(2) # -> Rational(1,2)
16
- #
17
- # See Rational for full documentation.
18
- #
1
+ # :enddoc:
19
2
 
20
-
21
- #
22
- # Creates a Rational number (i.e. a fraction). +a+ and +b+ should be Integers:
23
- #
24
- # Rational(1,3) # -> 1/3
25
- #
26
- # Note: trying to construct a Rational with floating point or real values
27
- # produces errors:
28
- #
29
- # Rational(1.1, 2.3) # -> NoMethodError
30
- #
31
- def Rational(a, b = 1)
32
- if a.kind_of?(Rational) && b == 1
33
- a
34
- else
35
- Rational.reduce(a, b)
36
- end
37
- end
38
-
39
- #
40
- # Rational implements a rational class for numbers.
41
- #
42
- # <em>A rational number is a number that can be expressed as a fraction p/q
43
- # where p and q are integers and q != 0. A rational number p/q is said to have
44
- # numerator p and denominator q. Numbers that are not rational are called
45
- # irrational numbers.</em> (http://mathworld.wolfram.com/RationalNumber.html)
46
- #
47
- # To create a Rational Number:
48
- # Rational(a,b) # -> a/b
49
- # Rational.new!(a,b) # -> a/b
50
- #
51
- # Examples:
52
- # Rational(5,6) # -> 5/6
53
- # Rational(5) # -> 5/1
54
- #
55
- # Rational numbers are reduced to their lowest terms:
56
- # Rational(6,10) # -> 3/5
57
- #
58
- # But not if you use the unusual method "new!":
59
- # Rational.new!(6,10) # -> 6/10
60
- #
61
- # Division by zero is obviously not allowed:
62
- # Rational(3,0) # -> ZeroDivisionError
63
- #
64
3
  class Rational < Numeric
65
- @RCS_ID='-$Id: rational.rb,v 1.7 1999/08/24 12:49:28 keiju Exp keiju $-'
66
-
67
- #
68
- # Reduces the given numerator and denominator to their lowest terms. Use
69
- # Rational() instead.
70
- #
71
- def Rational.reduce(num, den = 1)
72
- raise ZeroDivisionError, "denominator is zero" if den == 0
73
-
74
- if den < 0
75
- num = -num
76
- den = -den
77
- end
78
- gcd = num.gcd(den)
79
- num = num.div(gcd)
80
- den = den.div(gcd)
81
- if den == 1 && defined?(Unify)
82
- num
83
- else
84
- new!(num, den)
85
- end
86
- end
87
-
88
- #
89
- # Implements the constructor. This method does not reduce to lowest terms or
90
- # check for division by zero. Therefore #Rational() should be preferred in
91
- # normal use.
92
- #
93
- def Rational.new!(num, den = 1)
94
- new(num, den)
95
- end
96
-
97
- private_class_method :new
98
-
99
- #
100
- # This method is actually private.
101
- #
102
- def initialize(num, den)
103
- if den < 0
104
- num = -num
105
- den = -den
106
- end
107
- if num.kind_of?(Integer) and den.kind_of?(Integer)
108
- @numerator = num
109
- @denominator = den
110
- else
111
- @numerator = num.to_i
112
- @denominator = den.to_i
113
- end
114
- end
115
-
116
- #
117
- # Returns the addition of this value and +a+.
118
- #
119
- # Examples:
120
- # r = Rational(3,4) # -> Rational(3,4)
121
- # r + 1 # -> Rational(7,4)
122
- # r + 0.5 # -> 1.25
123
- #
124
- def + (a)
125
- if a.kind_of?(Rational)
126
- num = @numerator * a.denominator
127
- num_a = a.numerator * @denominator
128
- Rational(num + num_a, @denominator * a.denominator)
129
- elsif a.kind_of?(Integer)
130
- self + Rational.new!(a, 1)
131
- elsif a.kind_of?(Float)
132
- Float(self) + a
133
- else
134
- x, y = a.coerce(self)
135
- x + y
136
- end
137
- end
138
-
139
- #
140
- # Returns the difference of this value and +a+.
141
- # subtracted.
142
- #
143
- # Examples:
144
- # r = Rational(3,4) # -> Rational(3,4)
145
- # r - 1 # -> Rational(-1,4)
146
- # r - 0.5 # -> 0.25
147
- #
148
- def - (a)
149
- if a.kind_of?(Rational)
150
- num = @numerator * a.denominator
151
- num_a = a.numerator * @denominator
152
- Rational(num - num_a, @denominator*a.denominator)
153
- elsif a.kind_of?(Integer)
154
- self - Rational.new!(a, 1)
155
- elsif a.kind_of?(Float)
156
- Float(self) - a
157
- else
158
- x, y = a.coerce(self)
159
- x - y
160
- end
161
- end
162
-
163
- #
164
- # Returns the product of this value and +a+.
165
- #
166
- # Examples:
167
- # r = Rational(3,4) # -> Rational(3,4)
168
- # r * 2 # -> Rational(3,2)
169
- # r * 4 # -> Rational(3,1)
170
- # r * 0.5 # -> 0.375
171
- # r * Rational(1,2) # -> Rational(3,8)
172
- #
173
- def * (a)
174
- if a.kind_of?(Rational)
175
- num = @numerator * a.numerator
176
- den = @denominator * a.denominator
177
- Rational(num, den)
178
- elsif a.kind_of?(Integer)
179
- self * Rational.new!(a, 1)
180
- elsif a.kind_of?(Float)
181
- Float(self) * a
182
- else
183
- x, y = a.coerce(self)
184
- x * y
185
- end
186
- end
187
-
188
- #
189
- # Returns the quotient of this value and +a+.
190
- # r = Rational(3,4) # -> Rational(3,4)
191
- # r / 2 # -> Rational(3,8)
192
- # r / 2.0 # -> 0.375
193
- # r / Rational(1,2) # -> Rational(3,2)
194
- #
195
- def / (a)
196
- if a.kind_of?(Rational)
197
- num = @numerator * a.denominator
198
- den = @denominator * a.numerator
199
- Rational(num, den)
200
- elsif a.kind_of?(Integer)
201
- raise ZeroDivisionError, "division by zero" if a == 0
202
- self / Rational.new!(a, 1)
203
- elsif a.kind_of?(Float)
204
- Float(self) / a
205
- else
206
- x, y = a.coerce(self)
207
- x / y
208
- end
209
- end
210
-
211
- #
212
- # Returns this value raised to the given power.
213
- #
214
- # Examples:
215
- # r = Rational(3,4) # -> Rational(3,4)
216
- # r ** 2 # -> Rational(9,16)
217
- # r ** 2.0 # -> 0.5625
218
- # r ** Rational(1,2) # -> 0.866025403784439
219
- #
220
- def ** (other)
221
- if other.kind_of?(Rational)
222
- Float(self) ** other
223
- elsif other.kind_of?(Integer)
224
- if other > 0
225
- num = @numerator ** other
226
- den = @denominator ** other
227
- elsif other < 0
228
- num = @denominator ** -other
229
- den = @numerator ** -other
230
- elsif other == 0
231
- num = 1
232
- den = 1
233
- end
234
- Rational.new!(num, den)
235
- elsif other.kind_of?(Float)
236
- Float(self) ** other
237
- else
238
- x, y = other.coerce(self)
239
- x ** y
240
- end
241
- end
242
-
243
4
  def div(other)
244
- (self / other).floor
245
- end
246
-
247
- #
248
- # Returns the remainder when this value is divided by +other+.
249
- #
250
- # Examples:
251
- # r = Rational(7,4) # -> Rational(7,4)
252
- # r % Rational(1,2) # -> Rational(1,4)
253
- # r % 1 # -> Rational(3,4)
254
- # r % Rational(1,7) # -> Rational(1,28)
255
- # r % 0.26 # -> 0.19
256
- #
257
- def % (other)
258
- value = (self / other).floor
259
- return self - other * value
260
- end
261
-
262
- #
263
- # Returns the quotient _and_ remainder.
264
- #
265
- # Examples:
266
- # r = Rational(7,4) # -> Rational(7,4)
267
- # r.divmod Rational(1,2) # -> [3, Rational(1,4)]
268
- #
269
- def divmod(other)
270
- value = (self / other).floor
271
- return value, self - other * value
272
- end
273
-
274
- #
275
- # Returns the absolute value.
276
- #
277
- def abs
278
- if @numerator > 0
279
- self
280
- else
281
- Rational.new!(-@numerator, @denominator)
282
- end
283
- end
284
-
285
- #
286
- # Returns +true+ iff this value is numerically equal to +other+.
287
- #
288
- # But beware:
289
- # Rational(1,2) == Rational(4,8) # -> true
290
- # Rational(1,2) == Rational.new!(4,8) # -> false
291
- #
292
- # Don't use Rational.new!
293
- #
294
- def == (other)
295
- if other.kind_of?(Rational)
296
- @numerator == other.numerator and @denominator == other.denominator
297
- elsif other.kind_of?(Integer)
298
- self == Rational.new!(other, 1)
299
- elsif other.kind_of?(Float)
300
- Float(self) == other
301
- else
302
- other == self
303
- end
304
- end
305
-
306
- #
307
- # Standard comparison operator.
308
- #
309
- def <=> (other)
310
- if other.kind_of?(Rational)
311
- num = @numerator * other.denominator
312
- num_a = other.numerator * @denominator
313
- v = num - num_a
314
- if v > 0
315
- return 1
316
- elsif v < 0
317
- return -1
318
- else
319
- return 0
320
- end
321
- elsif other.kind_of?(Integer)
322
- return self <=> Rational.new!(other, 1)
323
- elsif other.kind_of?(Float)
324
- return Float(self) <=> other
325
- elsif defined? other.coerce
326
- x, y = other.coerce(self)
327
- return x <=> y
328
- else
329
- return nil
330
- end
331
- end
332
-
333
- def coerce(other)
334
- if other.kind_of?(Float)
335
- return other, self.to_f
336
- elsif other.kind_of?(Integer)
337
- return Rational.new!(other, 1), self
338
- else
339
- super
340
- end
341
- end
342
-
343
- #
344
- # Converts the rational to an Integer. Not the _nearest_ integer, the
345
- # truncated integer. Study the following example carefully:
346
- # Rational(+7,4).to_i # -> 1
347
- # Rational(-7,4).to_i # -> -1
348
- # (-1.75).to_i # -> -1
349
- #
350
- # In other words:
351
- # Rational(-7,4) == -1.75 # -> true
352
- # Rational(-7,4).to_i == (-1.75).to_i # -> true
353
- #
354
-
355
- def floor
356
- @numerator.div(@denominator)
357
- end
358
-
359
- def ceil
360
- -((-@numerator).div(@denominator))
361
- end
362
-
363
- def truncate
364
- if @numerator < 0
365
- return -((-@numerator).div(@denominator))
366
- end
367
- @numerator.div(@denominator)
368
- end
369
-
370
- alias_method :to_i, :truncate
371
-
372
- def round
373
- if @numerator < 0
374
- num = -@numerator
375
- num = num * 2 + @denominator
376
- den = @denominator * 2
377
- -(num.div(den))
378
- else
379
- num = @numerator * 2 + @denominator
380
- den = @denominator * 2
381
- num.div(den)
382
- end
383
- end
384
-
385
- #
386
- # Converts the rational to a Float.
387
- #
388
- def to_f
389
- @numerator.to_f/@denominator.to_f
390
- end
391
-
392
- #
393
- # Returns a string representation of the rational number.
394
- #
395
- # Example:
396
- # Rational(3,4).to_s # "3/4"
397
- # Rational(8).to_s # "8"
398
- #
399
- def to_s
400
- if @denominator == 1
401
- @numerator.to_s
402
- else
403
- @numerator.to_s+"/"+@denominator.to_s
404
- end
405
- end
406
-
407
- #
408
- # Returns +self+.
409
- #
410
- def to_r
411
- self
412
- end
413
-
414
- #
415
- # Returns a reconstructable string representation:
416
- #
417
- # Rational(5,8).inspect # -> "Rational(5, 8)"
418
- #
419
- def inspect
420
- sprintf("Rational(%s, %s)", @numerator.inspect, @denominator.inspect)
421
- end
422
-
423
- #
424
- # Returns a hash code for the object.
425
- #
426
- def hash
427
- @numerator.hash ^ @denominator.hash
428
- end
429
-
430
- attr :numerator
431
- attr :denominator
432
-
433
- private :initialize
434
- end
435
-
436
- class Integer
437
- #
438
- # In an integer, the value _is_ the numerator of its rational equivalent.
439
- # Therefore, this method returns +self+.
440
- #
441
- def numerator
442
- self
443
- end
444
-
445
- #
446
- # In an integer, the denominator is 1. Therefore, this method returns 1.
447
- #
448
- def denominator
449
- 1
450
- end
451
-
452
- #
453
- # Returns a Rational representation of this integer.
454
- #
455
- def to_r
456
- Rational(self, 1)
457
- end
458
-
459
- #
460
- # Returns the <em>greatest common denominator</em> of the two numbers (+self+
461
- # and +n+).
462
- #
463
- # Examples:
464
- # 72.gcd 168 # -> 24
465
- # 19.gcd 36 # -> 1
466
- #
467
- # The result is positive, no matter the sign of the arguments.
468
- #
469
- def gcd(other)
470
- min = self.abs
471
- max = other.abs
472
- while min > 0
473
- tmp = min
474
- min = max % min
475
- max = tmp
476
- end
477
- max
478
- end
479
-
480
- #
481
- # Returns the <em>lowest common multiple</em> (LCM) of the two arguments
482
- # (+self+ and +other+).
483
- #
484
- # Examples:
485
- # 6.lcm 7 # -> 42
486
- # 6.lcm 9 # -> 18
487
- #
488
- def lcm(other)
489
- if self.zero? or other.zero?
490
- 0
491
- else
492
- (self.div(self.gcd(other)) * other).abs
5
+ if other.is_a?(Float) && other == 0.0
6
+ raise ZeroDivisionError, "division by zero"
493
7
  end
494
- end
495
8
 
496
- #
497
- # Returns the GCD _and_ the LCM (see #gcd and #lcm) of the two arguments
498
- # (+self+ and +other+). This is more efficient than calculating them
499
- # separately.
500
- #
501
- # Example:
502
- # 6.gcdlcm 9 # -> [3, 18]
503
- #
504
- def gcdlcm(other)
505
- gcd = self.gcd(other)
506
- if self.zero? or other.zero?
507
- [gcd, 0]
508
- else
509
- [gcd, (self.div(gcd) * other).abs]
510
- end
9
+ (self / other).floor
511
10
  end
512
11
  end
513
12
 
514
13
  class Fixnum
515
- remove_method :quo
516
14
 
517
- # If Rational is defined, returns a Rational number instead of a Fixnum.
518
- def quo(other)
519
- Rational.new!(self, 1) / other
520
- end
15
+ alias quof fdiv
521
16
  alias rdiv quo
522
17
 
523
- # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0).
524
- def rpower (other)
525
- if other >= 0
526
- self.power!(other)
527
- else
528
- Rational.new!(self, 1)**other
529
- end
530
- end
18
+ alias power! ** unless method_defined? :power!
19
+ alias rpower **
20
+
531
21
  end
532
22
 
533
23
  class Bignum
534
- remove_method :quo
535
24
 
536
- # If Rational is defined, returns a Rational number instead of a Float.
537
- def quo(other)
538
- Rational.new!(self, 1) / other
539
- end
25
+ alias quof fdiv
540
26
  alias rdiv quo
541
27
 
542
- # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0).
543
- def rpower (other)
544
- if other >= 0
545
- self.power!(other)
546
- else
547
- Rational.new!(self, 1)**other
548
- end
549
- end
550
- end
28
+ alias power! ** unless method_defined? :power!
29
+ alias rpower **
551
30
 
552
- unless 1.respond_to?(:power!)
553
- class Fixnum
554
- alias_method :power!, :"**"
555
- alias_method :"**", :rpower
556
- end
557
-
558
- class Bignum
559
- alias_method :power!, :"**"
560
- alias_method :"**", :rpower
561
- end
562
31
  end
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Rational
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.1"
4
4
  end
5
5
  end
@@ -18,4 +18,5 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.add_development_dependency "bundler", "~> 1.3"
20
20
  spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
21
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-rational
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-28 00:00:00.000000000 Z
11
+ date: 2013-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
41
55
  description: Ruby standard library rational.
42
56
  email:
43
57
  - brixen@gmail.com
@@ -143,3 +157,4 @@ test_files:
143
157
  - spec/to_r_spec.rb
144
158
  - spec/to_s_spec.rb
145
159
  - spec/truncate_spec.rb
160
+ has_rdoc: