This commit is contained in:
blueloveTH 2023-06-15 04:19:40 +08:00
parent 439314dc97
commit 9625134e32
3 changed files with 21 additions and 7 deletions

View File

@ -175,15 +175,15 @@ class long:
else: else:
assert type(other) is long assert type(other) is long
if self.sign == other.sign: if self.sign == other.sign:
return long(ulong_add(self.digits, other.digits), self.sign) return long((ulong_add(self.digits, other.digits), self.sign))
else: else:
cmp = ulong_cmp(self.digits, other.digits) cmp = ulong_cmp(self.digits, other.digits)
if cmp == 0: if cmp == 0:
return long(0) return long(0)
if cmp > 0: if cmp > 0:
return long(ulong_sub(self.digits, other.digits), self.sign) return long((ulong_sub(self.digits, other.digits), self.sign))
else: else:
return long(ulong_sub(other.digits, self.digits), other.sign) return long((ulong_sub(other.digits, self.digits), other.sign))
def __radd__(self, other): def __radd__(self, other):
return self.__add__(other) return self.__add__(other)
@ -194,15 +194,15 @@ class long:
else: else:
assert type(other) is long assert type(other) is long
if self.sign != other.sign: if self.sign != other.sign:
return long(ulong_add(self.digits, other.digits), self.sign) return long((ulong_add(self.digits, other.digits), self.sign))
else: else:
cmp = ulong_cmp(self.digits, other.digits) cmp = ulong_cmp(self.digits, other.digits)
if cmp == 0: if cmp == 0:
return long(0) return long(0)
if cmp > 0: if cmp > 0:
return long(ulong_sub(self.digits, other.digits), self.sign) return long((ulong_sub(self.digits, other.digits), self.sign))
else: else:
return long(ulong_sub(other.digits, self.digits), -other.sign) return long((ulong_sub(other.digits, self.digits), -other.sign))
def __rsub__(self, other): def __rsub__(self, other):
return self.__sub__(other) return self.__sub__(other)
@ -270,7 +270,7 @@ class long:
raise NotImplementedError raise NotImplementedError
def __neg__(self): def __neg__(self):
return long(self.digits, -self.sign) return long((self.digits, -self.sign))
def __cmp__(self, other): def __cmp__(self, other):
if type(other) is int: if type(other) is int:

View File

@ -114,6 +114,7 @@ public:
PyObject* None; PyObject* None;
PyObject* True; PyObject* True;
PyObject* False; PyObject* False;
PyObject* NotImplemented; // unused
PyObject* Ellipsis; PyObject* Ellipsis;
PyObject* builtins; // builtins module PyObject* builtins; // builtins module
PyObject* StopIteration; PyObject* StopIteration;
@ -1125,6 +1126,7 @@ inline void VM::init_builtin_types(){
tp_star_wrapper = _new_type_object("_star_wrapper"); tp_star_wrapper = _new_type_object("_star_wrapper");
this->None = heap._new<Dummy>(_new_type_object("NoneType"), {}); this->None = heap._new<Dummy>(_new_type_object("NoneType"), {});
this->NotImplemented = heap._new<Dummy>(_new_type_object("NotImplementedType"), {});
this->Ellipsis = heap._new<Dummy>(_new_type_object("ellipsis"), {}); this->Ellipsis = heap._new<Dummy>(_new_type_object("ellipsis"), {});
this->True = heap._new<Dummy>(tp_bool, {}); this->True = heap._new<Dummy>(tp_bool, {});
this->False = heap._new<Dummy>(tp_bool, {}); this->False = heap._new<Dummy>(tp_bool, {});

12
tests/09_long.py Normal file
View File

@ -0,0 +1,12 @@
assert long(123) == long('123') == 123
a = long(2)
assert a ** 0 == 1
assert a ** 60 == 1152921504606846976
assert a + 1 == 3
assert a - 1 == 1
assert a * 2 == 4
assert a // 2 == 1
assert -a == -2