473,320 Members | 2,147 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,320 software developers and data experts.

integer division towards -infinity

Hi all,

As I understand it, the rounding direction of signed integer division
in C is unspecified in C89/C94, and specified to be 'towards-zero' in
C99.

I need division 'towards -infinity' (with a 'mod' operation to match),
i.e. for x and y I need to obtain D and M such that

(1) D*abs(y)+M == x
(2) 0 <= M<abs(y)

For any signed x,y (y!=0).

Is there a canonical way of doing this that is guaranteed to work in
line with all C specifications?

Best regards,

Sidney Cadot
The Netherlands
Nov 13 '05 #1
3 5398
On 10 Jul 2003 02:03:06 -0700, si****@jigsaw.nl (Sidney Cadot) wrote:
Hi all,

As I understand it, the rounding direction of signed integer division
in C is unspecified in C89/C94, and specified to be 'towards-zero' in
C99.

I need division 'towards -infinity' (with a 'mod' operation to match),
i.e. for x and y I need to obtain D and M such that

(1) D*abs(y)+M == x
(2) 0 <= M<abs(y)

For any signed x,y (y!=0).

Is there a canonical way of doing this that is guaranteed to work in
line with all C specifications?


Did you think of using div_t div(int, int)? It does not round to
minus infinity, but at least it rounds the same in both standards,
IIRC.

Best wishes,

Bob
Nov 13 '05 #2
On 10 Jul 2003, Sidney Cadot wrote:
Did you think of using div_t div(int, int)? It does not round to
minus infinity, but at least it rounds the same in both standards,
IIRC.


Indeed this can be used to solve my problem. However I thought of an
alternative this afternoon that is a bit terser:

div_to_minus_infinity = a/b-(a%b<0);
mod_to_minus_infinity = a%b+b*(a%b<0);


Negative b, round to zero:

div: -8/-5 - (-8%-5<0) == 1 - (-3<0) == 0
mod: -8%-5 + -5*(-8%-5<0) == -3 + -5*(-3<0) == -8

Fixed ones: (i think)

div: a/b - (a%b<0 ? (b<0 ? -1 : 1) : 0)
mod: a%b + (a%b<0 ? (b<0 ? -b : b) : 0)
Nov 13 '05 #3

"Sidney Cadot" <si****@jigsaw.nl> wrote in message
news:74**************************@posting.google.c om...
Hi Bob,
Did you think of using div_t div(int, int)? It does not round to
minus infinity, but at least it rounds the same in both standards,
IIRC.


Indeed this can be used to solve my problem. However I thought of an
alternative this afternoon that is a bit terser:

div_to_minus_infinity = a/b-(a%b<0);

This only assumes that the div and mod operators use equivalent
rounding rules, and works with both round-to-zero and
round-to-minus-infinity rules. In fact, it should even work with a
hypothetical round-to-positive-infinity implementation. I think it's
quite nifty :-)

Mod-to-minus-infinity would look like

mod_to_minus_infinity = a%b+b*(a%b<0);


I usually use (a%b+b)%b for the mod case. I never needed the divide case,
so I never tried that one.

-- glen
Nov 13 '05 #4

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

Similar topics

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...
24
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
13
by: Howard | last post by:
Hi, I know I can do integer division with the / operator, and get the modulus with the % operator, but is there any function that calculates both values in one shot? It seems quite wasteful in...
3
by: Janice | last post by:
I got this question from my textbook and I cannot understand the theory. When a signed positive integer X divided by pow(2,k), the result is shifting k bits to right and putting w-k bits of 0 from...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
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...
9
by: PengYu.UT | last post by:
Hi, The usually integer division will round the result to the biggest integet smaller than the float version division.For example, 10/3 = 3. I'm wondering if there is any easy way to round it...
9
by: jm.suresh | last post by:
Hello all, I have two integers and I want to divide one by another, and want to get an integer result which is the higher side whenever the result is a fraction. 3/2 =1 # Usual behavior...
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.