views:

48

answers:

2

why does model.diff return 18446744073709551615 in template, when model is like this and model.pos is 0 and model.neg is 1?:

class Kaart(models.Model):
    neg = models.PositiveIntegerField(default=0)
    pos = models.PositiveIntegerField(default=0)
    def diff(self):
     return self.pos - self.neg
A: 

You're getting underflow, where self.pos - self.neg should give -1, but you have a positive field, so it wraps around and you get 0 - 1 = 18446744073709551615, the largest positive number representable by PositiveIntegerField.

For your reference, 18446744073709551615 = 2**64 - 1, which means that PositiveIntegerField stores 64-bit values.

Peter
Actually, 18446744073709551615 is === 2^64-1.
Peter Rowell
heh, updated this just as you left your comment.
Peter
allright, but why does'nt x = self.pos - self.neg work any different? I mean, do i have to change field type just so the equation would work? thats kinda stupid, aint it, as x has not been described as variable?
Zayatzz
Thanks for responses everybody. It seems that the best thing to do was to change field type from PositiveIntegerField to IntegerField
Zayatzz
A: 

Peter's probably right. Try:

def diff(self):
    return int(self.pos) - int(self.neg)

That should do the trick.

Albert Peschar
Before posting i did try this: def diff(self): x = int(self.pos) - int(self.neg) return xWhich gives exactly the same result.
Zayatzz