Connecting Tech Pros Worldwide Forums | Help | Site Map

negative numbers and integer division

Matthew Wilson
Guest
 
Posts: n/a
#1: Jul 18 '05
Hi-

I just discovered this:
[color=blue][color=green][color=darkred]
>>> -1 // 12[/color][/color][/color]
-1[color=blue][color=green][color=darkred]
>>> 1 // 12[/color][/color][/color]
0[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

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?

Martin v. Löwis
Guest
 
Posts: n/a
#2: Jul 18 '05

re: negative numbers and integer division


"Matthew Wilson" <mwilson@sarcastic-horse.com> writes:
[color=blue]
> Hi-
>
> I just discovered this:
>[color=green][color=darkred]
> >>> -1 // 12[/color][/color]
> -1[color=green][color=darkred]
> >>> 1 // 12[/color][/color]
> 0[color=green][color=darkred]
> >>>[/color][/color]
>
> I thought that -1 // 12 would be 0 also.[/color]

Why did you think that? Dividing -1 by 12 gives -1, with a remainder
of 11: -1*12 + 11 == -1.
[color=blue]
> I'm writing a simple monthly date class and i need (-1,2001) to be
> translated to (11,2000). Any ideas?[/color]

def norm_month(month, year):
delta_year, month = divmod(month, 12)
return month, year+delta_year

print norm_month(-1, 2001)

Regards,
Martin
Mark Hahn
Guest
 
Posts: n/a
#3: Jul 18 '05

re: negative numbers and integer division


That definitely seems wrong to me, even though it is a correct "floor"
function.

I was given the impression that (int // int) was going to be the replacement
for (int / int) when (int / int) is changed to return a float, but -1/12 now
gives 0, not -1, so (int // int) is not a replacement for (int / int).

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


Mark Hahn
Guest
 
Posts: n/a
#4: Jul 18 '05

re: negative numbers and integer division


oops -- ignore that last post -- i was totally wrong

"Mark Hahn" <mark@hahnca.com> wrote in message
news:HWjfb.5187$hp5.1152@fed1read04...[color=blue]
> That definitely seems wrong to me, even though it is a correct "floor"
> function.
>
> I was given the impression that (int // int) was going to be the[/color]
replacement[color=blue]
> for (int / int) when (int / int) is changed to return a float, but -1/12[/color]
now[color=blue]
> gives 0, not -1, so (int // int) is not a replacement for (int / int).
>
> "Matthew Wilson" <mwilson@sarcastic-horse.com> wrote in message
> news:mailman.1065206646.19185.python-list@python.org...[color=green]
> > Hi-
> >
> > I just discovered this:
> >[color=darkred]
> > >>> -1 // 12[/color]
> > -1[color=darkred]
> > >>> 1 // 12[/color]
> > 0[color=darkred]
> > >>>[/color]
> >
> > 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[/color][/color]
ideas?[color=blue][color=green]
> >[/color]
>
>[/color]


Rainer Deyke
Guest
 
Posts: n/a
#5: Jul 18 '05

re: negative numbers and integer division


Matthew Wilson wrote:[color=blue]
> 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?[/color]

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@eldwood.com - http://eldwood.com


Skip Montanaro
Guest
 
Posts: n/a
#6: Jul 18 '05

re: negative numbers and integer division



Mark> I was given the impression that (int // int) was going to be the
Mark> replacement for (int / int) when (int / int) is changed to return
Mark> a float, but -1/12 now gives 0, not -1, so (int // int) is not a
Mark> replacement for (int / int).
[color=blue][color=green][color=darkred]
>>> from __future__ import division
>>> -1/12[/color][/color][/color]
-0.083333333333333329[color=blue][color=green][color=darkred]
>>> -1//12[/color][/color][/color]
-1

Skip

Peter Abel
Guest
 
Posts: n/a
#7: Jul 18 '05

re: negative numbers and integer division


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

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

Regards
Peter
mensanator
Guest
 
Posts: n/a
#8: Jul 18 '05

re: negative numbers and integer division


"Matthew Wilson" <mwilson@sarcastic-horse.com> wrote in message news:<mailman.1065206646.19185.python-list@python.org>...[color=blue]
> Hi-
>
> I just discovered this:
>[color=green][color=darkred]
> >>> -1 // 12[/color][/color]
> -1[color=green][color=darkred]
> >>> 1 // 12[/color][/color]
> 0[color=green][color=darkred]
> >>>[/color][/color]
>
> 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?[/color]
[color=blue][color=green][color=darkred]
>>> -1 % 12[/color][/color][/color]
11
Closed Thread