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
3
votes
1 answer

Knuth long division algorithm

I'm implementing algorithm D of section 4.3.2 of volume 2 of The Art of Computer Programming by D. E. Knuth. On step D3 I'm supposed to compute q = floor(u[j+n]*BASE+u[j+n-1] / v[n-1]) and r = u[j+n]*BASE+u[j+n-1] mod v[n-1]. Here, u (dividend) and…
user3368561
  • 779
  • 6
  • 18
3
votes
2 answers

Logarithmic time integer division using bit shift addition and subtraction only

I was asked to implement integer division with logarithmic time complexity using only bit shifts, additions and subtractions. I can see how I can deal with a divisor which is a power of 2, but how can I deal with an odd divisor, such that the time…
3
votes
6 answers

Efficiently dividing unsigned value by a power of two, rounding up - in CUDA

I was just reading: Efficiently dividing unsigned value by a power of two, rounding up and I was wondering what was the fastest way to do this in CUDA. Of course by "fast" I mean in terms of throughput (that question also addressed the case of…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
1 answer

Divide by zero exception on M0+ devices

Is there a Divide by zero exception on M0+ devices? I know Cortex M3 and M4 devices have this.
user1069620
3
votes
1 answer

Haskell - Strange truncation behavior of `div`

In ghci I get the following: λ> -1 `div` 2 0 However: λ> map (`div` 2) [-1] [-1] The problem arose when I was using a function divPair: divPair :: (Int, Int) -> Int -> (Int, Int) divPair (a, b) n = (a `div` n, b `div` n) λ> divPair (-1, -2)…
xji
  • 7,341
  • 4
  • 40
  • 61
3
votes
1 answer

How to randomly divide an integer into a fixed number of integers, such that the obtained tuples are uniformly distributed?

Based on this reply: Random numbers that add to 100: Matlab I tried to apply the suggested method to randomly divide an integer into a fixed number of integers whose sum is equal to the integer. Although that method seems to result in a uniformly…
Dave
  • 33
  • 4
3
votes
3 answers

Division by 1 in programming languages

So what if we tell a programming language to calculate this: X/1, where X is any number. Do they actually calculate the output or check/ignore 1 and return X? Furthermore, when coding something like the above is it worth checking if the divider is…
czioutas
  • 1,032
  • 2
  • 11
  • 37
3
votes
1 answer

Runtime error dividing by -1

I don't doubt the need to check for division by zero. I've never heard of checking for division by negative one though! if( *y == 0 ) return 0; //undefined else return *x / *y; x, y are pointers to int32_t, I include this detail in case of…
OJFord
  • 10,522
  • 8
  • 64
  • 98
3
votes
4 answers

Python round with `n // 1`

I was wondering if there is any reason not to use the // operator to round a number to an integer. I didn't see much on this topic or really know what to look for to find out more. >>> from random import random >>> random() * 20 // 1 1.0 >>>…
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
3
votes
2 answers

Divide two integers to get a float in MySQL

I'm working on a user defined function in MySQL that takes an integer (mediumint) and divides it by 100 (i.e. 10785 / 100 = 107.85). The supplied integer is a parameter called time. I've tried DECLARE dtime FLOAT; SET dtime = time / 100; DECLARE…
Robbert
  • 6,481
  • 5
  • 35
  • 61
3
votes
1 answer

Floating Point Division in System Verilog

I wanted to use floating point numbers in System Verilog using the real data type. I tried the following code, but it doesn't seem to work. I'm getting 2.000000 where I expect 2.500000. Module: module real_check(input [31:0]a, [31:0]b, …
3
votes
1 answer

Precise division of doubles representing integers exactly (when they are divisible)

Given that 8-byte doubles can represent all 4-byte ints precisely, I'm wondering whether dividing a double A storing an int, by a double B storing an int (such that the integer B divides A) will always give the exact double corresponding to the…
Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
3
votes
4 answers

Remainder after division by zero

I know that division by zero is not allowed in math, but can I use modulo by zero and what answer should I get? For example 10%0 = ? 5%0 = ?
user3394586
  • 191
  • 1
  • 1
  • 6
3
votes
10 answers

Difference in x/2 and x>>1 or x*2 and x << 1 where x is an integer

As we know for calculating an integer x/2 we just writey=x/2;similarly for x*2; but good programmers use bit manipulation to calculate this. They just do y = x >> 1; Is there any difference between these two method at all? By difference I mean…
user2897690
3
votes
3 answers

Why division value of -ve int is different from +ve one?

I tried this over Python. I am aware that using ints and doing their division will only return the integer value of quotient, truncating the decimal part. On this note: 3 / 2 = 1 Sounds perfectly all right... However when the dividend is negative,…
BhaveshDiwan
  • 669
  • 10
  • 22