Sha256: 6b1c5156ae3ffae5579d0fe38e57d1f6f51a2a39c91896b82f16c0e355a5bb12

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

= Radix Rational

  require 'radix'

== Initialization

Radix::Rational's initializer takes a numerator and a denominator,
either of which can be an Integer, Float, String or Array along witha
an integer base.

Give a integer value, it will automatically be converted to the base
specified.

  check do |num, dem, base, eqf|
    r = Radix::Rational.new(num, dem, base)
    r.assert == eqf
  end

  ok  1, 2,  2,  0.5
  ok  1, 1,  2,  1.0

  ok  8, 1, 10,  8.0
  ok  8, 5, 10,  1.6
  ok  8, 8, 10,  1.0

  ok 10, 1, 10, 10.0
  ok 10, 2, 10,  5.0
  ok 10, 5, 10,  2.0

  ok  8, 1, 16,  8.0
  ok 16, 1, 16, 16.0

== Reduction

  check do |a, x|
    r = a.reduce
    r.assert == x
  end

  ok [10,5].br(10),  [2,1].br(10)
  ok [30,3].br(10), [10,1].br(10)

== Operations

=== Addition

  check do |a, b, x|
    r = a + b
    r.assert == x
  end

  ok [8,5].br(10), [1,2].br(10), [21,10].br(10)

  ok [8,5].br(10), 1, [13,5].br(10)

  ok [8,5].br(10), 0.5, [21,10].br(10)

=== Subtraction

  check do |a, b, x|
    r = a - b
    r.assert == x
  end

  ok [8,5].br(10), [1,2].br(10), [11,10].br(10)

=== Multiplication

  check do |a, b, x|
    r = a * b
    r.assert == x
  end

  ok [8,5].br(10), [1,2].br(10), [8,10].br(10)

=== Division

  check do |a, b, x|
    r = a / b
    r.assert == x
  end

  ok [8,5].br(10), [1,2].br(10), [16,5].br(10)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radix-2.0.0 test/04_rational.rdoc