Hi, All, i am trying write a calculator, now normal calculation works ok. but if i enter long numbers, for example 333333333333333333 + 7, it gives me "333333333333333312".....
there are 18 "3"s, if the length is more than 17, the calculation will be wrong on Windows and Ubuntu. the following is some of my codes: def pre_calc(self, op): #if entry is empty, any operators are invalid except "-" if not self.entry.get_text() and op != "-": return if not self.entry.get_text() and op == "-": self.entry.set_text(op) self.cleared = 1 self.negative_sign = True#indicates there's already a "-" return #retrieve the 1st num and operator if not self.op and self.entry.get_text(): if op != "=": self.op = op #print "text typ:", type(self.entry.get_text()) self.first_num = self.entry.get_text() self.cleared = 0 # ready to input 2nd num #print "op:", self.op, "-----", "1st num:", self.first_num return else: self.first_num = None self.second_num = None self.op = None self.cleared = 0 self.negative_sign = False return #retrieve the second num and begin calculation elif self.op and self.first_num and self.entry.get_text() != "-": if op == "=": self.second_num = self.entry.get_text() self.calc() #reset the following varibles, awaiting next round of inputs self.first_num = None self.second_num = None self.op = None self.cleared = 0 self.negative_sign = False return elif op == "-": if self.cleared == 1: #if user has input 2nd num already self.second_num = self.entry.get_text() self.calc() self.first_num = self.entry.get_text() self.op = op self.cleared = 0 self.negative_sign = False return else: self.entry.set_text(op) self.cleared = 1 self.negative_sign = True#indicates there's already a "-" return else: self.op = op if self.cleared == 1: self.second_num = self.entry.get_text() self.calc() self.first_num = self.entry.get_text() self.cleared = 0 self.negative_sign = False return else: return def calc(self, unary_op = None): if not unary_op: if (not self.first_num) or (not self.second_num) or \ (not self.op): return result = None calc_methods = {"+": self.add, "-": self.subtract, "*": self.multiply, "/": self.divide, "%": self.mod, "pwr": self.power, "|": self.b_or, "&": self.b_and, "^": self.b_exc, "<<": self.b_lshift, ">>": self.b_rshift } unary_methods ={"~": self.b_invert, "1/x": self.a_fraction, "sqrt": self.sqrt } if not unary_op: for op, method in calc_methods.items(): if self.op == op: result = calc_methods[op]() else: for op, method in unary_methods.items(): if unary_op == op: result = unary_methods[op]() self.entry.set_text(result) def add(self): result = None #if hex system #print type(self.first_num) if self.hdob["h"].get_active(): self.first_num = long(self.first_num, 16) self.second_num = long(self.second_num, 16) r = self.first_num + self.second_num r = self.convert.d2h(r) return r #if decimal system elif self.hdob["d"].get_active(): self.first_num = float(self.first_num) self.second_num = float(self.second_num) result = self.first_num + self.second_num if result - long(result) == 0: #print str(long(result)) return str(long(result)) else: return str(result) #if octal system elif self.hdob["o"].get_active(): self.first_num = long(self.first_num, 8) self.second_num = long(self.second_num, 8) result = self.first_num + self.second_num result = self.convert.d2o(result) return result else: self.first_num = long(self.first_num, 2) self.second_num = long(self.second_num, 2) result = bin(self.first_num + self.second_num) result = self.convert.d2b(result) return result but if i run the following codes on command line, it works ok: >>>a = 33333333333333333333333333333333333333L >>>b= str(a) >>>b '33333333333333333333333333333333333333' why? i don't understand.... anybody can help me? thanks. Lion
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor