Sha256: 4cef5cfb4ed84086053d2337e9c9cac84a58c917772621cb708a8f50fe3712b3

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 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

4 entries across 4 versions & 2 rubygems

Version Path
radix-2.2.1 demo/04_rational.md
radix-firstbanco-2.2.0 demo/04_rational.md
radix-2.2.0 demo/04_rational.md
radix-2.1.1 demo/04_rational.md