Commit c14215c5 authored by Boxiang Sun's avatar Boxiang Sun

some small bug fixing

parent e3979f05
...@@ -95,7 +95,7 @@ extern "C" Box* abs_(Box* x) { ...@@ -95,7 +95,7 @@ extern "C" Box* abs_(Box* x) {
return boxInt(n >= 0 ? n : -n); return boxInt(n >= 0 ? n : -n);
} else if (x->cls == float_cls) { } else if (x->cls == float_cls) {
double d = static_cast<BoxedFloat*>(x)->d; double d = static_cast<BoxedFloat*>(x)->d;
return boxFloat(d >= 0 ? d : -d); return boxFloat(std::abs(d));
} else if (x->cls == long_cls) { } else if (x->cls == long_cls) {
return longAbs(static_cast<BoxedLong*>(x)); return longAbs(static_cast<BoxedLong*>(x));
} else { } else {
......
...@@ -760,6 +760,14 @@ Box* floatPos(BoxedFloat* self) { ...@@ -760,6 +760,14 @@ Box* floatPos(BoxedFloat* self) {
return PyFloat_FromDouble(self->d); return PyFloat_FromDouble(self->d);
} }
Box* floatAbs(BoxedFloat* self) {
if (!PyFloat_Check(self))
raiseExcHelper(TypeError, "descriptor '__abs__' requires a 'float' object but received a '%s'",
getTypeName(self));
double res = std::abs(self->d);
return boxFloat(res);
}
bool floatNonzeroUnboxed(BoxedFloat* self) { bool floatNonzeroUnboxed(BoxedFloat* self) {
assert(self->cls == float_cls); assert(self->cls == float_cls);
return self->d != 0.0; return self->d != 0.0;
...@@ -1614,6 +1622,7 @@ void setupFloat() { ...@@ -1614,6 +1622,7 @@ void setupFloat() {
float_cls->giveAttr("__gt__", new BoxedFunction(FunctionMetadata::create((void*)floatGt, UNKNOWN, 2))); float_cls->giveAttr("__gt__", new BoxedFunction(FunctionMetadata::create((void*)floatGt, UNKNOWN, 2)));
float_cls->giveAttr("__neg__", new BoxedFunction(FunctionMetadata::create((void*)floatNeg, BOXED_FLOAT, 1))); float_cls->giveAttr("__neg__", new BoxedFunction(FunctionMetadata::create((void*)floatNeg, BOXED_FLOAT, 1)));
float_cls->giveAttr("__pos__", new BoxedFunction(FunctionMetadata::create((void*)floatPos, BOXED_FLOAT, 1))); float_cls->giveAttr("__pos__", new BoxedFunction(FunctionMetadata::create((void*)floatPos, BOXED_FLOAT, 1)));
float_cls->giveAttr("__abs__", new BoxedFunction(FunctionMetadata::create((void*)floatAbs, BOXED_FLOAT, 1)));
FunctionMetadata* nonzero = FunctionMetadata::create((void*)floatNonzeroUnboxed, BOOL, 1); FunctionMetadata* nonzero = FunctionMetadata::create((void*)floatNonzeroUnboxed, BOOL, 1);
nonzero->addVersion((void*)floatNonzero, UNKNOWN); nonzero->addVersion((void*)floatNonzero, UNKNOWN);
......
...@@ -919,6 +919,14 @@ extern "C" Box* intInt(BoxedInt* self) { ...@@ -919,6 +919,14 @@ extern "C" Box* intInt(BoxedInt* self) {
return boxInt(self->n); return boxInt(self->n);
} }
Box* intFloat(BoxedInt* self) {
if (!PyInt_Check(self))
raiseExcHelper(TypeError, "descriptor '__float__' requires a 'int' object but received a '%s'",
getTypeName(self));
return boxFloat(self->n);
}
extern "C" Box* intIndex(BoxedInt* v) { extern "C" Box* intIndex(BoxedInt* v) {
if (PyInt_CheckExact(v)) if (PyInt_CheckExact(v))
return v; return v;
...@@ -1205,6 +1213,7 @@ void setupInt() { ...@@ -1205,6 +1213,7 @@ void setupInt() {
int_cls->giveAttr("__trunc__", new BoxedFunction(FunctionMetadata::create((void*)intTrunc, BOXED_INT, 1))); int_cls->giveAttr("__trunc__", new BoxedFunction(FunctionMetadata::create((void*)intTrunc, BOXED_INT, 1)));
int_cls->giveAttr("__index__", new BoxedFunction(FunctionMetadata::create((void*)intIndex, BOXED_INT, 1))); int_cls->giveAttr("__index__", new BoxedFunction(FunctionMetadata::create((void*)intIndex, BOXED_INT, 1)));
int_cls->giveAttr("__int__", new BoxedFunction(FunctionMetadata::create((void*)intInt, BOXED_INT, 1))); int_cls->giveAttr("__int__", new BoxedFunction(FunctionMetadata::create((void*)intInt, BOXED_INT, 1)));
int_cls->giveAttr("__float__", new BoxedFunction(FunctionMetadata::create((void*)intFloat, BOXED_FLOAT, 1)));
auto int_new = FunctionMetadata::create((void*)intNew<CXX>, UNKNOWN, 3, false, false, auto int_new = FunctionMetadata::create((void*)intNew<CXX>, UNKNOWN, 3, false, false,
ParamNames({ "", "x", "base" }, "", ""), CXX); ParamNames({ "", "x", "base" }, "", ""), CXX);
......
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