Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.rational

This module contains the Rational type, which is used to represent rational numbers, along with related mathematical operations.

Authors:
Arlen Aghakians

License:
Boost License 1.0

Source:
std/rational.d

pure @safe auto rational(N)(N num);
pure @safe auto rational(N, D)(N num, D den);
Helper function that returns a rational number with the specified numerator and denominator.

This function returns a Rational!long if neither num nor den are signed integrals; otherwise, the return type is deduced using std.traits.CommonType!(N, D).

Examples:
   auto r1 = rational(2,4);
   assert(r1.num == 1);
   assert(r1.den == 2);
   assert(r1.value == 0.5)

   auto r2 = r1 * rational(4,3);
   auto s = r2.toString();
   assert(s == "(2/3)");

   r2 /= 3;
   assert(r2 == rational(2,9));

struct Rational(T) if (isSignedIntegral!(T));
A rational number parameterised by a type T, which must be a signed integral. The rational number is reduced to its simplest form. Rational is more efficient if both the numerator and the denominator are immutable because it is simplified only once in the constructor.

const void toString(scope void delegate(const(char)[]) sink, string fmt = "%s");
void toString(scope void delegate(const(char)[]) sink, string fmt = "%s");
Converts the rational number to a string representation.

const pure nothrow @safe T num();
Returns the numerator of the rational number.

const pure nothrow @safe T den();
Returns the denominator of the rational number.

pure nothrow @safe T num();
Returns the numerator of the rational number. The rational number is simplified if it is not already in simplified form. This method is available only if the numerator and the denominator are both mutable.

pure nothrow @safe T den();
Returns the denominator of the rational number. The rational number is simplified if it is not already in simplified form. This method is available only if the numerator and the denominator are both mutable.

pure nothrow @safe void num(T n);
Sets the numerator to n.

pure nothrow @safe void den(T d);
Sets the denominator to d.

pure nothrow @safe this(T num, T den = 1);

const pure nothrow @safe F value(F = double)();
Converts the rational number to a floating-point representation.

const pure nothrow @safe bool opEquals(G)(Rational!(G) other);

const pure nothrow @safe bool opEquals(G)(G i);

const pure nothrow @safe int opCmp(G)(Rational!(G) other);

const pure nothrow @safe int opCmp(G)(G i);

const pure nothrow @safe Rational opUnary(string op)();

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinary(string op, G)(Rational!(G) other);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinary(string op, G)(G i);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinaryRight(string op, G)(G i);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinaryRight(string op, G)(G i);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinary(string op, G)(Rational!(G) other);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinary(string op, G)(G i);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinary(string op, G)(Rational!(G) other);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinary(string op, G)(G i);

const pure nothrow @safe Rational!(CommonType!(T, G)) opBinaryRight(string op, G)(G i);

pure nothrow @safe Rational opOpAssign(string op, G)(Rational!(G) other);

pure nothrow @safe Rational opOpAssign(string op, G)(G i);

template isRational(T)
Check to see if T is a Rational.

pure @safe Rational!(G) abs(G)(Rational!(G) r);
Calculates the absolute value.

Examples:
   auto a = rational(-2,4);
   assert(abs(a) == rational(1,2));

pure nothrow @safe G ceil(G)(Rational!(G) r);
Returns the truncated value (towards positive infinity).

Examples:
   assert(ceil(rational(7,5)) == 2);
   assert(ceil(rational(-5,3)) == -1);

pure nothrow @safe G floor(G)(Rational!(G) r);
Returns the truncated value (towards negative infinity).

Examples:
   assert(floor(rational(7,5)) == 1);
   assert(floor(rational(-5,3)) == -2);

pure nothrow @safe G round(G)(Rational!(G) r);
Returns the truncated value (towards the nearest integer; (1/2) => 1; (-1/2) => -1).

Examples:
   assert(round(rational(7,5)) == 1);
   assert(round(rational(-7,5)) == -1);