This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,47 @@
import std.stdio, std.conv;
struct Number(T) {
T x; // Base.
alias x this;
string toString() const { return text(x); }
Number opBinary(string op)(in int exponent)
const pure nothrow if (op == "^^") in {
if (exponent < 0)
assert (x != 0, "Division by zero");
} body {
debug puts("opBinary ^^");
int zerodir;
T factor;
if (exponent < 0) {
zerodir = +1;
factor = (cast(T)1) / x;
} else {
zerodir = -1;
factor = x;
}
T result = 1;
int e = exponent;
while (e != 0)
if (e % 2 != 0) {
result *= factor;
e += zerodir;
} else {
factor *= factor;
e /= 2;
}
return Number(result);
}
}
void main() {
alias Double = Number!double;
writeln(Double(2.5) ^^ 5);
alias Int = Number!int;
writeln(Int(3) ^^ 3);
writeln(Int(0) ^^ -2); // Division by zero.
}