negative numbers and integer division

Peter Abel PeterAbel at gmx.net
Fri Oct 3 17:02:00 EDT 2003


"Matthew Wilson" <mwilson at sarcastic-horse.com> wrote in message news:<mailman.1065206646.19185.python-list at python.org>...
> Hi-
> 
> I just discovered this:
> 
> >>> -1 // 12
>  -1
> >>> 1 // 12
>  0
> >>>
> 
> 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?

Maybe the following could help:
>>> def normalize_date(month,year):
... 	return (((month+11) % 12)+1,year+((month-1)/12))
... 
>>> normalize_date(1,2003)
(1, 2003)
>>> normalize_date(12,2003)
(12, 2003)
>>> normalize_date(0,2003)
(12, 2002)
>>> normalize_date(-1,2003)
(11, 2002)
>>> normalize_date(13,2003)
(1, 2004)
>>> 

Regards
Peter




More information about the Python-list mailing list