negative numbers and integer division
Rainer Deyke
rainerd at eldwood.com
Fri Oct 3 16:03:12 EDT 2003
Matthew Wilson wrote:
> I thought that -1 // 12 would be 0 also. I'm writing a simple monthly
> date class and i need (-1,2001) to be translated to (11,2000). Any
> ideas?
Luckily for you, Python integer division does the right thing. You
*want* -1 // 12 to be -1 for your application.
def normalize_date(month, year):
return (month % 12, year + month // 12)
normalize_date(-1, 2001) # Returns (11, 2000)
'normalize_date' as given assumes 'month' is in the range from 0 to 11; use
this if your months are in the range from 1 to 12:
def normalize_date(month, year):
return ((month - 1) % 12 + 1, year + (month - 1) // 12)
--
Rainer Deyke - rainerd at eldwood.com - http://eldwood.com
More information about the Python-list
mailing list