473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

negative numbers and integer division

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?

Jul 18 '05 #1
7 2627
"Matthew Wilson" <mw*****@sarcastic-horse.com> writes:
Hi-

I just discovered this:
-1 // 12 -1 1 // 12 0

I thought that -1 // 12 would be 0 also.


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


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

print norm_month(-1, 2001)

Regards,
Martin
Jul 18 '05 #2
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" <mw*****@sarcastic-horse.com> wrote in message
news:ma**********************************@python.o rg...
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?

Jul 18 '05 #3
oops -- ignore that last post -- i was totally wrong

"Mark Hahn" <ma**@hahnca.com> wrote in message
news:HWjfb.5187$hp5.1152@fed1read04...
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" <mw*****@sarcastic-horse.com> wrote in message
news:ma**********************************@python.o rg...
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?


Jul 18 '05 #4
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 - ra*****@eldwood.com - http://eldwood.com
Jul 18 '05 #5

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).
from __future__ import division
-1/12 -0.083333333333333329 -1//12

-1

Skip

Jul 18 '05 #6
"Matthew Wilson" <mw*****@sarcastic-horse.com> wrote in message news:<ma**********************************@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
Jul 18 '05 #7
"Matthew Wilson" <mw*****@sarcastic-horse.com> wrote in message news:<ma**********************************@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?

-1 % 12

11
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP 4.3.1 on both systems. Here is my program,...
19
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
4
by: August Karlstrom | last post by:
Hi, How come the modulus operator doesn't always yield non-negative values? I expect e.g. (-1) % 3 to be 2 but I get -1. If I want to decrement a cyclic variable n by k I have to write something...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
11
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
8
by: valentin tihomirov | last post by:
My rateonale is: -1 mod 3 = must be 3 0 mod 3 = 0 1 mod 3 = 1 2 mod 3 = 2 3 mod 3 = 3 4 mod 3 = 0 = 1 = 2 = 3
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.