inv

Undocumented in source.
@safe pure nothrow pragma(inline, true)
alias inv = inverse

Examples

construction and assignment

Q x = null;
assert(x.numerator == 0);
assert(x.denominator == 1);

const Q y = Q(11, 13UL);
assert(y.numerator == 11);
assert(y.denominator == 13);

const Q z = Q(7UL, 13UL);
assert(z.numerator == 7);
assert(z.denominator == 13);

Q w = 0.25;                 // construct from `double`
assert(w.numerator == 1);
assert(w.denominator == 4);

w = 0.125f;                 // assign from `float`
assert(w.numerator == 1);
assert(w.denominator == 8);

w = 2;                      // assign from `int`
assert(w.numerator == 2);
assert(w.denominator == 1);

w = 3;                      // assign from `int`
assert(w.numerator == 3);
assert(w.denominator == 1);

canonicalization

Q x = Q(2, 4);
assert(x.numerator == 2);
assert(x.denominator == 4);
x.canonicalize();
assert(x.numerator == 1);
assert(x.denominator == 2);

negative numerator canonicalization

Q x = Q(-2, 4);
assert(x.numerator == -2);
assert(x.denominator == 4);
x.canonicalize();
assert(x.numerator == -1);
assert(x.denominator == 2);

swap

Q x = Q(1, 2);
Q y = Q(1, 3);
swap(x, y);
assert(x == Q(1, 3));
assert(y == Q(1, 2));

invert

Q x = Q(1, 2);
assert(x.numerator == 1);
assert(x.denominator == 2);

x.invert();
assert(x.numerator == 2);
assert(x.denominator == 1);

Q y = Q(-1, 2);

// TODO:
// assert(x.numerator == -1);
// assert(x.denominator == 2);

// x.invert();
// assert(x.numerator == -2);
// assert(x.denominator == 1);

inversion

const Q q = Q(-2, 3);
assert(inverse(q) == Q(-3, 2));

assert(inverse(Q(2, 3)) == Q(3, 2));
assert(inverse(Q(1, 10)) == 10);
assert(inverse(Q(10, 1)) == Q(1, 10));

absolute value

const Q q = Q(-2, 3);
assert(abs(q) == Q(2, 3));
assert(abs(Q(-2, 3)) == Q(2, 3));

integer and fractional part

Q x = Q(5, 2);

assert(x.integerPart == 2);
// TODO: assert(x.fractionalPart == Q(1, 2));

x = Q(7, 2);
assert(x.integerPart == 3);
// TODO: assert(x.fractionalPart == Q(1, 2));

x = Q(10, 2);
assert(x.integerPart == 5);
// TODO: assert(x.fractionalPart == 0);

x = Q(11, 3);
assert(x.integerPart == 3);
// TODO: assert(x.fractionalPart == Q(2, 3));

x = Q(12, 2);
assert(x.integerPart == 6);
// TODO: assert(x.fractionalPart == 0);

casting

assert(cast(double)Q(1, 2) == 0.5f);
assert(cast(double)Q(1, 2) == 0.5);
assert(cast(double)Q(2, 4) == 0.5);
assert(cast(double)Q(1, 8) == 1.0/8);

equality

assert(Q(1, 1) == 1);
assert(Q(2, 1) == 2);
assert(Q(1, 1) == Q(1, 1));
assert(Q(1, 1) != Q(1, 2));
const x = Q(1, 3);
assert(x == x);             // same

sign

assert(Q(-1, 3).sgn == -1);
assert(Q( 0, 3).sgn ==  0);
assert(Q( 1, 3).sgn ==  1);

comparison

assert(Q( 1, 3) < Q(1, 2));
assert(Q( 1, 2) > Q(1, 3));
assert(Q( 1, 2) > Q(0, 1));
assert(Q( 0, 1) == Q(0, 1));
assert(Q( 0, 2) == Q(0, 1));
assert(Q(-1, 2) < Q(0, 1));

assert(Q( 1, 3) < 1);
assert(Q( 1, 3) > 0);
assert(Q(-1, 3) < 0);

assert(Q( 1, 3) < 1UL);
assert(Q( 1, 3) > 0UL);
assert(Q(-1, 3) < 0UL);

assert(Q( 1, 3) < 1.Z);
assert(Q( 1, 3) > 0.Z);
assert(Q(-1, 3) < 0.Z);

addition

assert(Q(1, 2) + Q(1, 2) == Q(1, 1));
assert(Q(1, 3) + Q(1, 3) == Q(2, 3));
assert(Q(1, 2) + Q(1, 3) == Q(5, 6));

subtraction

assert(Q(1, 2) - Q(1, 2) == Q( 0, 1));
assert(Q(1, 2) - Q(1, 3) == Q (1, 6));
assert(Q(1, 3) - Q(1, 2) == Q(-1, 6));

multiplication

assert(Q(1, 2) * Q(1, 2) == Q(1, 4));
assert(Q(2, 3) * Q(2, 3) == Q(4, 9));
assert(Q(1, 2) * Q(1, 3) == Q(1, 6));

division

assert(Q(2, 3) / Q(2, 3) == Q(1, 1));
assert(Q(2, 3) / Q(2, 3) == 1);
assert(Q(2, 3) / Q(3, 2) == Q(4, 9));
assert(Q(3, 2) / Q(2, 3) == Q(9, 4));
// TODO: assert(1 / Q(2, 3) == Q(3, 2));

Meta