Anonymous user
Arithmetic/Rational: Difference between revisions
Updated D entry
(Added Maple implementation) |
(Updated D entry) |
||
Line 368:
=={{header|D}}==
Rational implementation based on BigInt. Currently this is not fast.
<lang d>import std.bigint, std.traits;
Line 480:
}
}▼
int opEquals(T)(/*in*/ T r) /*const pure nothrow*/ {▼
Rational rhs = Rational(r);
if (type() == Type.NaRAT || rhs.type() == Type.NaRAT)▼
return false;▼
return num == rhs.num && den == rhs.den;
}
int opCmp(T)(/*in*/ T r) /*const pure nothrow*/ {
if (type() == Type.NaRAT || rhs.type() == Type.NaRAT)
throw new Exception("Compare invlove an NaRAT.");
Line 490 ⟶ 501:
BigInt diff = num * rhs.den - den * rhs.num;
return (diff == 0) ? 0 : ((diff < 0) ? -1 : 1);
▲ }
▲ int opEquals(T)(/*in*/ T r) /*const pure nothrow*/ {
▲ Rational rhs = Rational(r);
▲ if (type() == Type.NaRAT || rhs.type() == Type.NaRAT)
▲ return false;
▲ return num == rhs.num && den == rhs.den;
}
|