Jump to content

Arithmetic/Rational: Difference between revisions

Updated D entry
(Improved D entry)
(Updated D entry)
Line 370:
<lang d>import std.bigint, std.traits, std.conv;
 
// std.numeric.gcd doesn't work with BigInt.
T gcd(T)(/*in*/ T a, /*in*/ T b) pure /*pure nothrow*/ {
// std.numeric.gcd doesn't work with BigInt.
return (b != 0) ? gcd(b, a % b) : (a < 0) ? -a : a;
}
 
T lcm(T)(/*in*/ T a, /*in*/ T b) pure /*nothrow*/ {
return a / gcd(a, b) * b;
}
 
struct RationalT(T) {
/*const*/ private T num, den; // Numerator & denominator.
 
private enum Type { NegINF = -2,
Line 398:
}
 
this(U, V)(/*in*/ U n, /*in*/ V d) pure /*pure nothrow*/ {
num = toT(n);
den = toT(d);
/*const*/ T common = gcd(num, den);
if (common != 0) {
num /= common;
Line 415:
}
 
static T toT(U)(/*in*/ ref U n) pure nothrow if (is(U == T)) {
return n;
}
Line 424:
}
 
T nomerator() /*const*/ pure nothrow @property {
return num;
}
 
T denominator() /*const*/ pure nothrow @property {
return den;
}
 
string toString() const /*pure constnothrow*/ {
if (den != 0)
return num.text ~ (den == 1 ? "" : "/" ~ den.text);
Line 441:
}
 
real toReal() pure const /*nothrow*/ {
static if (is(T == BigInt))
return num.toLong / cast(real)den.toLong;
else
return num / cast(real)den;
}
 
RationalT opBinary(string op)(/*in*/ RationalT r)
/*const pure /*nothrow*/ if (op == "+" || op == "-") {
T common = lcm(den, r.den);
T n = mixin("common / den * num" ~ op ~
Line 453 ⟶ 456:
}
 
RationalT opBinary(string op)(/*in*/ RationalT r)
/*const pure /*nothrow*/ if (op == "*") {
return RationalT(num * r.num, den * r.den);
}
 
RationalT opBinary(string op)(/*in*/ RationalT r)
/*const pure /*nothrow*/ if (op == "/") {
return RationalT(num * r.den, den * r.num);
}
 
RationalT opBinary(string op, U)(in U r)
/*const pure /*nothrow*/ if (isIntegral!U && (op == "+" ||
op == "-" || op == "*" || op == "/")) {
return opBinary!op(RationalT(r));
}
 
RationalT opBinary(string op)(in size_t p)
/*const pure /*nothrow*/ if (op == "^^") {
return RationalT(num ^^ p, den ^^ p);
}
 
RationalT opBinaryRight(string op, U)(in U l)
/*const pure /*nothrow*/ if (isIntegral!U) {
return RationalT(l).opBinary!op(RationalT(num, den));
}
 
RationalT opOpAssign(string op, U)(/*in*/ U l) pure /*nothrow*/ {
/*const pure nothrow*/ {
mixin("this = this " ~ op ~ "l;");
return this;
Line 486 ⟶ 488:
 
RationalT opUnary(string op)()
/*const pure /*nothrow*/ if (op == "+" || op == "-") {
return RationalT(mixin(op ~ "num"), den);
}
Line 494 ⟶ 496:
}
 
int opEquals(U)(/*in*/ U r) /*const pure nothrow*/ {
RationalT rhs = RationalT(r);
if (type() == Type.NaRAT || rhs.type() == Type.NaRAT)
Line 501 ⟶ 503:
}
 
int opCmp(U)(/*in*/ U r) /*const pure nothrow*/ {
auto rhs = RationalT(r);
if (type() == Type.NaRAT || rhs.type() == Type.NaRAT)
Line 513 ⟶ 515:
}
 
Type type() /*const pure nothrow*/ {
if (den > 0) return Type.NORMAL;
if (den < 0) return Type.NegDEN;
Cookies help us deliver our services. By using our services, you agree to our use of cookies.