Rational

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

Constructors

this
this(U n)
this(U n, V d, bool nonReduce)

ditto

Members

Functions

opAssign
void opAssign(U v)
void opAssign(Rational!U r)
opBinary
auto opBinary(Rational!U r)
auto opBinary(U v)
opBinaryRight
auto opBinaryRight(U v)
opCast
bool opCast()
U opCast()
opCmp
auto opCmp(U r)
auto opCmp(Rational!U r)
opEquals
bool opEquals(U v)
bool opEquals(Rational!U r)
opOpAssign
void opOpAssign(Rational!U r)
void opOpAssign(U v)
opUnary
typeof(this) opUnary()
toString
void toString(void delegate(const(char)[]) sink, FormatSpec!char fmt)

operator

Properties

den
inout(T) den [@property getter]
U den [@property setter]

denominator

num
inout(T) num [@property getter]
U num [@property setter]

numerator

reciprocal
typeof(this) reciprocal [@property getter]

return reciprocal number

Static functions

init
typeof(this) init()
Undocumented in source. Be warned that the author may not have intended to support it.

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