rational

This is the type that you can calculate fraction. Rational!T has two integral T values.

  1. Rational!(Unqual!(CommonType!(T, U))) rational(T num, U den)
    pure nothrow @safe
    Rational!(Unqual!(CommonType!(T, U)))
    rational
    (
    T
    U
    )
    (
    T num
    ,
    U den
    )
    if (
    isLikeBuiltInInt!(Unqual!(CommonType!(T, U)))
    )
  2. Rational!(Unqual!(CommonType!(T, U))) rational(T num, U den)
  3. Rational!(Unqual!T) rational(T num)
  4. Rational!(Unqual!T) rational(T num)

Examples

auto r = rational(10, 2);       // If you called rational(n, d), value is reduced.
assert(r.num == 5);             // 10 / 2 => 5 / 1
assert(r.den == 1);

assert(r == rational(5));       // rational(5) == rational(5, 1)

assert(r == 5.over(1));          // UFCS : n.over(d) == n.rational(d) == rational(n, d)

r *= -1.over(5);
assert(r.num == -1);            // If rational value is negative, numerator is always negative.
assert(r.den == 1);             // But denominator is always positive.
assert(r == rational(1, 1));    // (5 / 1) * (1 / 5) == (1 / 1)
assert(r == 1);                 // Can check equality to T by "==" operator.
assert(r > 2);                  // Also comparison operator.

r1 = 2.over(5) + 3;              // You can use Rational!T like T.

import std.bigint;
Rational!BigInt rb = 10.over(33);// You can use BigInt as T.
rb ^^= -10;
assert(rb == Rational!BigInt(BigInt(33)^^10, BigInt(10)^^10));

If T can be operated in pure nothrow @safe function, Rational!T can be too.

void foo() pure nothrow @safe
{
    auto r = rational(1, 3);    //int is pure nothrow @safe type
    r += 3.over(4);
    ...
}

You can use "%(...%)" format when formatted write. Where inner format "..." can be T's format, first one is numerator's format, second is denominator's format.

import std.format;

void main(){
    auto writer = appender!string;

    formattedWrite(writer, "%(%04d / %04d%)", rational(10, 33));
    assert(writer.data == "0010 / 0033");

    writer = appender!string;
    formattedWrite(writer, "%(den : %2$s , num : %1$s%)", rational(10, 33));
    assert(writer.data == "den : 33 , num : 10");

    writer = appender!string;
    formattedWrite(writer, "%04d", rational(10, 30));
    assert(writer.data == "0010/0030");
}

Meta