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:
}
 
intbool opCmpopCast(T)(/*in*/ T r) /*const pureif nothrow*/(is(T == bool)) {
return num == rhs.num && den =!= rhs.den0;
}
 
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*/ {
Rationalauto rhs = Rational(r);
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;
}