473,698 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2641
"Matthew Wilson" <mw*****@sarcas tic-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(mont h, 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*****@sarcas tic-horse.com> wrote in message
news:ma******** *************** ***********@pyt hon.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?

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

"Mark Hahn" <ma**@hahnca.co m> wrote in message
news:HWjfb.5187 $hp5.1152@fed1r ead04...
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*****@sarcas tic-horse.com> wrote in message
news:ma******** *************** ***********@pyt hon.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?


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.0833333333333 33329 -1//12

-1

Skip

Jul 18 '05 #6
"Matthew Wilson" <mw*****@sarcas tic-horse.com> wrote in message news:<ma******* *************** ************@py thon.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+((mo nth-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*****@sarcas tic-horse.com> wrote in message news:<ma******* *************** ************@py thon.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
1890
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, test.php. <?php $a = 0x81234567; $b = 0xF0000002; printx($a, $b);
19
5163
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 amazing, modulos of negative number give negative values! (in c). from an algebraic point of view, python seems right, but I thought python conformity to the underlying c compiler was a strong commitment, obviously not here, and I found no...
5
6116
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 and negative quantities . Can any one tell me what are the things i should bear in mind .
11
2388
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 compiler and got the same erroneous result on two different PCs (with OS Win98 & Win98SE), so it appears to be a problem with ANSI C. I thought that negative double variables could be compared as easily and *reliably* as integers, but apparently...
4
4954
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 like n = ((n - k) % length + length) % length where as in e.g. Pascal i can simply write
15
35496
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
3727
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 2nd and 3rd to last, I can enter x etc. This is fine. Logically, I should be able to enter x to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I...
39
4007
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, and that it contains no padding. Firstly, I realise that the MSB is known as the sign-bit, and that it indicates whether the number is positive or negative (irrespective of which negative number system is used).
8
5730
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
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.