~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/rational.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-05-16 12:37:06 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080516123706-r4llcdfd35aobrjv
Tags: 1.9.0.1-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.
* debian/control:
  - ruby1.9 pkg: moved rdoc1.9 suggestion to depends. (LP: #228345)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#   rational.rb -
3
3
#       $Release Version: 0.5 $
4
4
#       $Revision: 1.7 $
5
 
#       $Date: 1999/08/24 12:49:28 $
6
5
#       by Keiju ISHITSUKA(SHL Japan Inc.)
7
6
#
8
7
# Documentation by Kevin Jackson and Gavin Sinclair.
239
238
    end
240
239
  end
241
240
 
 
241
  def div(other)
 
242
    (self / other).floor
 
243
  end
 
244
 
242
245
  #
243
246
  # Returns the remainder when this value is divided by +other+.
244
247
  #
250
253
  #   r % 0.26             # -> 0.19
251
254
  #
252
255
  def % (other)
253
 
    value = (self / other).to_i
 
256
    value = (self / other).floor
254
257
    return self - other * value
255
258
  end
256
259
 
262
265
  #   r.divmod Rational(1,2)   # -> [3, Rational(1,4)]
263
266
  #
264
267
  def divmod(other)
265
 
    value = (self / other).to_i
 
268
    value = (self / other).floor
266
269
    return value, self - other * value
267
270
  end
268
271
 
271
274
  #
272
275
  def abs
273
276
    if @numerator > 0
274
 
      Rational.new!(@numerator, @denominator)
 
277
      self
275
278
    else
276
279
      Rational.new!(-@numerator, @denominator)
277
280
    end
346
349
  #   Rational(-7,4) == -1.75                 # -> true
347
350
  #   Rational(-7,4).to_i == (-1.75).to_i     # false
348
351
  #
349
 
  def to_i
350
 
    Integer(@numerator.div(@denominator))
 
352
 
 
353
  def floor()
 
354
    @numerator.div(@denominator)
 
355
  end
 
356
 
 
357
  def ceil()
 
358
    -((-@numerator).div(@denominator))
 
359
  end
 
360
 
 
361
  def truncate()
 
362
    if @numerator < 0
 
363
      return -((-@numerator).div(@denominator))
 
364
    end
 
365
    @numerator.div(@denominator)
 
366
  end
 
367
 
 
368
  alias_method :to_i, :truncate
 
369
 
 
370
  def round()
 
371
    if @numerator < 0
 
372
      num = -@numerator
 
373
      num = num * 2 + @denominator
 
374
      den = @denominator * 2
 
375
      -(num.div(den))
 
376
    else
 
377
      num = @numerator * 2 + @denominator
 
378
      den = @denominator * 2
 
379
      num.div(den)
 
380
    end
351
381
  end
352
382
 
353
383
  #
477
507
 
478
508
class Fixnum
479
509
  alias quof quo
480
 
  undef quo
481
 
  # If Rational is defined, returns a Rational number instead of a Fixnum.
 
510
  remove_method :quo
 
511
 
 
512
  # If Rational is defined, returns a Rational number instead of a Float.
482
513
  def quo(other)
483
 
    Rational.new!(self,1) / other
 
514
    Rational.new!(self, 1) / other
484
515
  end
485
516
  alias rdiv quo
486
517
 
489
520
    if other >= 0
490
521
      self.power!(other)
491
522
    else
492
 
      Rational.new!(self,1)**other
 
523
      Rational.new!(self, 1)**other
493
524
    end
494
525
  end
495
 
 
496
 
  unless defined? 1.power!
497
 
    alias power! **
498
 
    alias ** rpower
499
 
  end
500
526
end
501
527
 
502
528
class Bignum
503
 
  unless defined? Complex
504
 
    alias power! **
505
 
  end
506
 
 
507
529
  alias quof quo
508
 
  undef quo
509
 
  # If Rational is defined, returns a Rational number instead of a Bignum.
 
530
  remove_method :quo
 
531
 
 
532
  # If Rational is defined, returns a Rational number instead of a Float.
510
533
  def quo(other)
511
 
    Rational.new!(self,1) / other
 
534
    Rational.new!(self, 1) / other
512
535
  end
513
536
  alias rdiv quo
514
537
 
520
543
      Rational.new!(self, 1)**other
521
544
    end
522
545
  end
 
546
end
523
547
 
524
 
  unless defined? Complex
 
548
unless defined? 1.power!
 
549
  class Fixnum
 
550
    alias power! **
 
551
    alias ** rpower
 
552
  end
 
553
  class Bignum
 
554
    alias power! **
525
555
    alias ** rpower
526
556
  end
527
557
end