Commit fa4bf00c authored by Marius Wachtler's avatar Marius Wachtler

Teach abs how to handle longs

this let's us import random
parent 71620917
......@@ -90,6 +90,8 @@ extern "C" Box* abs_(Box* x) {
} else if (x->cls == float_cls) {
double d = static_cast<BoxedFloat*>(x)->d;
return boxFloat(d >= 0 ? d : -d);
} else if (x->cls == long_cls) {
return longAbs(static_cast<BoxedLong*>(x));
} else {
RELEASE_ASSERT(0, "%s", getTypeName(x)->c_str());
}
......
......@@ -224,6 +224,14 @@ Box* longNeg(BoxedLong* v1) {
return r;
}
Box* longAbs(BoxedLong* v1) {
assert(isSubclass(v1->cls, long_cls));
BoxedLong* r = new BoxedLong(long_cls);
mpz_init(r->n);
mpz_abs(r->n, v1->n);
return r;
}
Box* longAdd(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__add__' requires a 'long' object but received a '%s'",
......
......@@ -37,6 +37,7 @@ extern "C" Box* createLong(const std::string* s);
extern "C" BoxedLong* boxLong(int64_t n);
Box* longNeg(BoxedLong* lhs);
Box* longAbs(BoxedLong* v1);
Box* longAdd(BoxedLong* lhs, Box* rhs);
Box* longSub(BoxedLong* lhs, Box* rhs);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment