Anonymous user
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;
T gcd(T)(
▲ // std.numeric.gcd doesn't work with BigInt.
return (b != 0) ? gcd(b, a % b) : (a < 0) ? -a : a;
}
T lcm(T)(
return a / gcd(a, b) * b;
}
struct RationalT(T) {
private enum Type { NegINF = -2,
Line 398:
}
this(U, V)(
num = toT(n);
den = toT(d);
if (common != 0) {
num /= common;
Line 415:
}
static T toT(U)(
return n;
}
Line 424:
}
T nomerator()
return num;
}
T denominator()
return den;
}
string toString() const /*pure
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)(
T common = lcm(den, r.den);
T n = mixin("common / den * num" ~ op ~
Line 453 ⟶ 456:
}
RationalT opBinary(string op)(
return RationalT(num * r.num, den * r.den);
}
RationalT opBinary(string op)(
return RationalT(num * r.den, den * r.num);
}
RationalT opBinary(string op, U)(in U r)
return opBinary!op(RationalT(r));
}
RationalT opBinary(string op)(in size_t p)
return RationalT(num ^^ p, den ^^ p);
}
RationalT opBinaryRight(string op, U)(in U l)
return RationalT(l).opBinary!op(RationalT(num, den));
}
RationalT opOpAssign(string op, U)(
mixin("this = this " ~ op ~ "l;");
return this;
Line 486 ⟶ 488:
RationalT opUnary(string op)()
return RationalT(mixin(op ~ "num"), den);
}
Line 494 ⟶ 496:
}
int opEquals(U)(
RationalT rhs = RationalT(r);
if (type() == Type.NaRAT || rhs.type() == Type.NaRAT)
Line 501 ⟶ 503:
}
int opCmp(U)(
auto rhs = RationalT(r);
if (type() == Type.NaRAT || rhs.type() == Type.NaRAT)
Line 513 ⟶ 515:
}
Type type()
if (den > 0) return Type.NORMAL;
if (den < 0) return Type.NegDEN;
|