Questions tagged [integer-division]

Anything related to the integer division operation, i.e. that special form of division which is performed between two integer numbers and which in math results in a quotient and a remainder. This is mostly relevant for languages which have specific integer data-types, for which the division is an integer division, or for languages having a specific operator/function to perform integer division.

Anything related to the integer division operation, i.e. that special form of division which is performed between two integer numbers and which in math results in a quotient and a remainder. This is mostly relevant for languages which have specific integer data-types, for which the division is an integer division, or for languages having a specific operator/function to perform integer division.

619 questions
4
votes
4 answers

C remainder/modulo operator definition for positive arguments

I found a function in a program I am supposed to fix that has a mod function defined: int mod(int a, int b) { int i = a%b; if(i<0) i+=b; return i; } I was told that a and b will always be positive by the way... Huh? if(i<0)? The argument is,…
towi
  • 21,587
  • 28
  • 106
  • 187
4
votes
4 answers

32-bit / 16-bit signed integer division without 32-bit registers?

I'm trying to divide a 32-bit signed integer by a 16-bit signed integer to get a signed 32-bit quotient and 16-bit remainder. I'm targeting a 286 with no fpu. I've already written code in the past to do 32-bit unsigned division: DIV32 PROC ;DIVIDES…
bad
  • 939
  • 6
  • 18
4
votes
1 answer

Handling long integer-divisions in python

I observed data loss in python integer division. Below is a sample: In [37]: 1881676377427221798261593926550420634004875213170503083 * 123456789789456123 Out[37]: 232305724979817822068561403710502859128427941904411569030164388864727209 In [38]:…
shripal mehta
  • 401
  • 4
  • 21
4
votes
1 answer

X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?

Can anyone explain for me why the sign of the remainder is different in these cases? Is this an emulator bug or do real CPUs do this, too? 8 / -3 : quotient(AL) = -2 remainder(AH) = 2 -8 / 3 : quotient(AL) = -2 remainder(AH) = -2
4
votes
1 answer

Why A / is faster when A is unsigned vs signed?

I have been reading through the Optimizing C++ wikibook. In the faster operations chapter one of the advice is as follows: Integer division by a constant When you divide an integer (that is known to be positive or zero) by a constant, convert the…
bergercookie
  • 2,542
  • 1
  • 30
  • 38
4
votes
1 answer

Detecting integer division in python

I have to make the Python 2 to 3 jump. Integer division is the only thing that has me scared. Our test suite is OK, but not necessarily set up to scrutinize division. Is there some sort of wrapper or environment or hack from which I could run…
Eli S
  • 1,379
  • 4
  • 14
  • 35
4
votes
1 answer

Is this a clever or stupid way to do an integer divide function?

I'm a Computer Science major, interested in how assembly languages handle a integer divide function. It seems that simply adding up to the numerator, while giving both the division and the mod, is way too impractical, so I came up with another way…
Curious
  • 41
  • 1
4
votes
1 answer

How to optimize for dividing a constant dividend?

Optimization for divided by a constant is well optimized by gcc, as is well known :) Now I wonder how dividing a constant is optimized. gcc does not help me out, and so does clang. Maybe I am not good at searching such information, but I cannot find…
Thiner
  • 345
  • 1
  • 9
4
votes
1 answer

Integer division between an Image taken from a VideoCapture of OpenCV and a scalar

I'm using version OpenCV 3.2. I open the default camera: VideoCapture cap; cap.open(0); and grab the current frame: Mat frame; cap.read(frame); Now I want to reduce the number of colours shown in the image. Thus, I do an integer division and…
Santiago Gil
  • 1,292
  • 7
  • 21
  • 52
4
votes
5 answers

Why byte and short division results in int in Java?

In Java, if we divide bytes, shorts or ints, we always get an int. If one of the operands is long, we'll get long. My question is - why does byte or short division not result in byte or short? Why always int? Apparently I'm not looking for the…
lexicore
  • 42,748
  • 17
  • 132
  • 221
4
votes
3 answers

How to keep precision on int64_t = int64_t * float?

I would like to perform a correction on an int64_t by a factor in the range [0.01..1.2] with precision is about 0.01. The naive implementation would be: int64_t apply_correction(int64_t y, float32_t factor) { return y *…
nowox
  • 25,978
  • 39
  • 143
  • 293
4
votes
3 answers

Can integer division be rounded up, instead of down?

Is there a way to round the result of integer division up to the nearest integer, rather than down? For example, I would like to change the default behavior: irb(main):001:0> 5 / 2 => 2 To the following behavior: irb(main):001:0> 5 / 2 => 3
ray
  • 641
  • 2
  • 8
  • 13
4
votes
2 answers

32 bit signed integer division gives 0x7fffffff as quotient on PowerPC

I am debugging a production code written in C and its simplest form can be shown as - void test_fun(int sr) { int hr = 0; #define ME 65535 #define SE 256 sr = sr/SE; <-- This should yield 0 if(sr == 1) …
4
votes
2 answers

How to check if a integer is sum of given integers?

Lets say I have a integer result and an array of integers, lets say [a,b,c] (not a fixed length). I need to detect if result=a*i +b*j + c*k, with i,j,k>=0. I prefer a solution in C/C# if it is possible. PS The problem is from a reservation system,…
user350887
  • 41
  • 3
4
votes
4 answers

Removing slow int64 division from fixed point atan2() approximation

I made a function to compute a fixed-point approximation of atan2(y, x). The problem is that of the ~83 cycles it takes to run the whole function, 70 cycles (compiling with gcc 4.9.1 mingw-w64 -O3 on an AMD FX-6100) are taken entirely by a simple…
Michel Rouzic
  • 1,013
  • 1
  • 9
  • 22