nxt.rational

This module contains an implementation of rational numbers that is templated on the underlying integer type. It can be used with either builtin fixed width integers or arbitrary precision integers. All relevant operators are overloaded for both rational-rational and rational-integer operations.

Synopsis:

// Compute pi using the generalized continued fraction approximation.
import std.bigint, std.rational, std.stdio;

enum maxTerm = 30;

Rational!(BigInt) getTerm(int termNumber)
{
	 auto addFactor = 2 * termNumber - 1;

	 if (termNumber == maxTerm)
	 {
		 return rational(BigInt(addFactor));
	 }

	 auto termNumberSquared = BigInt(termNumber * termNumber);
	 auto continued = termNumberSquared / getTerm(termNumber + 1);

	 continued += addFactor;
	 return continued;
}

void main()
{
	 auto pi = rational(BigInt(4)) / getTerm(1);

	 // Display the result in rational form.
	 writeln(pi);

	 // Display the decimal equivalent, which is accurate to 18 decimal places.
	 writefln("%.18f", cast(real) pi);
}

Members

Classes

OverflowException
class OverflowException
Undocumented in source.

Enums

isRational
eponymoustemplate isRational(T)

Checks if T has the basic properties of a rational type, i.e. it has a numerator and a denominator.

Functions

ceil
SomeIntegral ceil(Rational!SomeIntegral r)

Returns the smallest integer greater than or equal to r.

floor
SomeIntegral floor(Rational!SomeIntegral r)

Returns the largest integer less than or equal to r.

gcf
CommonInteger!(I1, I2) gcf(I1 m, I2 n)

Find the Greatest Common Factor (GCF), aka Greatest Common Divisor (GCD), of m and n.

lcm
CommonInteger!(I1, I2) lcm(I1 a, I2 b)

Find the Least Common Multiple (LCM) of a and b.

rational
Rational!(CommonInteger!(I1, I2)) rational(I1 i1, I2 i2)
Rational!(I) rational(I val)

Implements rational numbers on top of whatever integer type is specified by the user. The integer type used may be any type that behaves as an integer. Specifically, isIntegerLike must return true, the integer type must have value semantics, and the semantics of all integer operations must follow the normal rules of integer arithmetic.

round
SomeIntegral round(Rational!SomeIntegral r)

Round r to the nearest integer. If the fractional part is exactly 1/2, r will be rounded such that the absolute value is increased by rounding.

toRational
Rational!(SomeIntegral) toRational(real floatNum, real epsilon)

Convert a floating point number to a Rational based on integer type SomeIntegral. Allows an error tolerance of epsilon. (Default epsilon = 1e-8.)

Structs

Rational
struct Rational(SomeIntegral)

The struct that implements rational numbers. All relevant operators (addition, subtraction, multiplication, division, exponentiation by a non-negative integer, equality and comparison) are overloaded. The second operand for all binary operators except exponentiation may be either another Rational or another integer type.

Templates

isIntegerLike
template isIntegerLike(T)

Checks whether T is structurally an integer, i.e. whether it supports all of the operations an integer type should support. Does not check the nominal type of T. In particular, for a mutable type T the following must compile:

Meta

Authors

David Simcha