473,386 Members | 1,679 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,386 software developers and data experts.

Integer dicision

How does (a/b) work when both 'a' and 'b' are pure integers ?
>(9/2)
4
>(-9/2)
-5

Why is it -5 ? I expect it to be -4 ? Because, in C/C++, 9/2 is 4 and
so negative of it, (-9/2) is -4.

What should I do to get C-like behavior ?
Apr 11 '08 #1
5 1579
bdsatish wrote:
How does (a/b) work when both 'a' and 'b' are pure integers ?
>>(9/2)
4
>>(-9/2)
-5

Why is it -5 ? I expect it to be -4 ? Because, in C/C++, 9/2 is 4 and
so negative of it, (-9/2) is -4.

What should I do to get C-like behavior ?
Use C?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Apr 11 '08 #2
jim

it rounds down. 4 is less than 4.5
and -5 is less than -4.5.

On Thu, 2008-04-10 at 21:28 -0700, bdsatish wrote:
How does (a/b) work when both 'a' and 'b' are pure integers ?
(9/2)
4
(-9/2)
-5

Why is it -5 ? I expect it to be -4 ? Because, in C/C++, 9/2 is 4 and
so negative of it, (-9/2) is -4.

What should I do to get C-like behavior ?
Apr 11 '08 #3
On Apr 10, 9:28 pm, bdsatish <bdsat...@gmail.comwrote:
How does (a/b) work when both 'a' and 'b' are pure integers ?
Python defines the quotient and remainder from integer division so
that a = qb + r and 0<=r < abs(b). C/C++ lets the remainder be
negative.
>>divmod(-9,2)
(-5, 1)
>>divmod(9,2)
(4, 1)

casevh
Apr 11 '08 #4
On Apr 11, 6:06*am, casevh <cas...@gmail.comwrote:
On Apr 10, 9:28 pm, bdsatish <bdsat...@gmail.comwrote:
How does (a/b) work when both 'a' and 'b' are pure integers ?

Python defines the quotient and remainder from integer division so
that a = qb + r and 0<=r < abs(b). C/C++ lets the remainder be
negative.
On Apr 11, 6:06 am, casevh <cas...@gmail.comwrote:
On Apr 10, 9:28 pm, bdsatish <bdsat...@gmail.comwrote:
How does (a/b) work when both 'a' and 'b' are pure integers ?

Python defines the quotient and remainder from integer division so
that a = qb + r and 0<=r < abs(b). C/C++ lets the remainder be
negative.
On Apr 11, 6:06 am, casevh <cas...@gmail.comwrote:
On Apr 10, 9:28 pm, bdsatish <bdsat...@gmail.comwrote:
How does (a/b) work when both 'a' and 'b' are pure integers ?

Python defines the quotient and remainder from integer division so
that a = qb + r and 0<=r < abs(b). C/C++ lets the remainder be
negative.
(Puts language lawyer hat on)

That's not accurate: r can be negative. To quote the reference manual:
'The modulo operator always yields a result with the same sign as its
second operand (or zero); the absolute value of the result is strictly
smaller than the absolute value of the second operand.'

divmod(9, -2) # (-5, -1)

Both C and Python define q = a / b and r = a % b to satisfy a = q * b
+ r, where -abs(b) < r < abs(b).

Where they differ:
Python: r has the same sign of b (or 0).
C99: r has the same sign as a (or 0).
C89 (Standard C): It's implementation defined what sign r has if
either a or b is negative.

This means python already has C-like behaviour... it's compatible with
standard C, although not with C99.

--
Paul Hankin
Apr 11 '08 #5
bdsatish <bd******@gmail.comwrote:
How does (a/b) work when both 'a' and 'b' are pure integers ?
>>(9/2)
4
>>(-9/2)
-5

Why is it -5 ? I expect it to be -4 ? Because, in C/C++, 9/2 is 4 and
so negative of it, (-9/2) is -4.
Some background on the situation:

Integer division and remainder operators have to satisfy two axioms:

y * (x/y) + x%y = x

and

|x%y| < |y|

(The former is just the definition of remainder, and the latter is
necessary to get Euclid's algorithm to work.) When x and y are both
nonnegative it's easy to agree on the right behaviour. When x or y is
negative then we get conflicting requirements.

On the one hand, you get people who expect that (-x)/y == -(x/y).

On the other hand, you get people who expect 0 <= x%y < y if y >= 0.

Unfortunately, you can't have both and still satisfy the integer-
division axioms. C89 didn't specify which behaviour you got. C99 is in
the first camp. Python picked the second (long before C99 came out).

Which is right? I don't think that's actually a well-posed question:
both have uses. I certainly find myself using the latter behaviour
(floor, or round towards -infinity) almost exclusively, but then I'm
mainly doing number theory and cryptography, and I find the bounds on
the remainder very convenient.

Heedful of this mess, Common Lisp provides four (!) different integer
division functions (in addition to `/', which does exact rational
division on integers):

* (floor X Y) -Q R, where Q is the largest integer such that Q <=
X/Y, and R = X - Q Y; R is also available as (mod X Y).

* (ceiling X Y) -Q R, where Q is the smallest integer such that Q >=
X/Y, and R = X - Q Y.

* (truncate X Y) -Q R, where Q is the integer with the greatest
magnitude such that Q has the same sign as X/Y (or is zero) and |Q|
<= |X/Y|; again R = X - Q Y; R is also available as (rem X Y).

* (round X Y) -Q R, where Q is the nearest integer to X/Y (rounding
ties towards even numbers), and R = X - Q Y.

Gluing all this into Python is tricky, partly because the plethora of
options seems somewhat unPythonic (at least to me), and partly because
it relies on Common Lisp's behaviour of throwing away unwanted
additional return values.
What should I do to get C-like behavior ?
I would have said

abs(x) / abs(y) * sgn(x) * sgn(y)

but Python doesn't seem to have a signum function. :-(

I'd recommend thinking carefully about your problem and seeing whether
the existing floor-divide behaviour can't be made to fit (or indeed if
it's not actually better anyway). If that still doesn't help then
you'll have to solve the problem the hard way.

def trunc_divmod(x, y):
"""
Return truncating quotient and remainder for X/Y.

Assumes Y 0.
"""
q, r = divmod(x, y)
if x < 0 and r != 0:
r -= y
q += 1
return q, r

-- [mdw]
Jun 27 '08 #6

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

Similar topics

11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
11
by: nephish | last post by:
Hello there, i need a way to check to see if a certain value can be an integer. I have looked at is int(), but what is comming out is a string that may be an integer. i mean, it will be formatted...
21
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me....
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
1
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a...
7
by: laura | last post by:
Hi, I have a variable of type double. I need to know if there is an integer number store there. How can I test that ? I also have a default precision for doing this operation. Many thanks,...
9
by: jacob navia | last post by:
Hi I am incorporating 128 Bit integer code into lcc-win and it would be nice to have some code to test this feature. Has anyone here code that uses 128 bit integers? Thanks in advance ...
14
by: Default User | last post by:
Hi, If I have three 64 bit integers and I want to do this operation on them: x*y/z Lets say that what we are multiplying by (y) is offset by what we are dividing by (z) so that the final...
9
by: tsuyois | last post by:
Hi, I just signed in to this excellent network. I hope I could get some answers to many questions I have in writing C compilers. My first question is: Is "integer demotion" required in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.