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
65
votes
7 answers

How should I do integer division in Perl?

What is a good way to always do integer division in Perl? For example, I want: real / int = int int / real = int int / int = int
Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
64
votes
4 answers

Assembly Language - How to do Modulo?

Is there something like a modulo operator or instruction in x86 assembly?
enne87
  • 2,221
  • 8
  • 32
  • 61
64
votes
5 answers

Negative integer division surprising result

In my application I encountered the following and was surprised by the results: 8/-7=-2 (both integers). What does this mean?
Vivek S
  • 5,384
  • 8
  • 51
  • 72
61
votes
3 answers

What is the reason for difference between integer division and float to int conversion in python?

I have recently noticed that int() rounds a float towards 0, while integer division rounds a float towards its floor. for instance: -7 // 2 == -4 int(-7/2) == -3 I have read the documentation which specifies: class int(x, base=10) Return an…
Isdj
  • 1,835
  • 1
  • 18
  • 36
61
votes
9 answers

Divide by 10 using bit shifts?

Is it possible to divide an unsigned integer by 10 by using pure bit shifts, addition, subtraction and maybe multiply? Using a processor with very limited resources and slow divide.
Thomas O
  • 6,026
  • 12
  • 42
  • 60
49
votes
6 answers

Why does integer division round down in many scripting languages?

In the languages I have tested, - (x div y ) is not equal to -x div y; I have tested // in Python, / in Ruby, div in Perl 6; C has a similar behavior. That behavior is usually according to spec, since div is usually defined as the rounding down of…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
47
votes
8 answers

In Python, what is a good way to round towards zero in integer division?

1/2 gives 0 as it should. However, -1/2 gives -1 , but I want it to round towards 0 (i.e. I want -1/2 to be 0), regardless of whether it's positive or negative. What is the best way to do that?
blacktrance
  • 905
  • 2
  • 11
  • 25
45
votes
3 answers

Why is such complex code emitted for dividing a signed integer by a power of two?

When I compile this code with VC++10: DWORD ran = rand(); return ran / 4096; I get this disassembly: 299: { 300: DWORD ran = rand(); 00403940 call dword ptr [__imp__rand (4050C0h)] 301: return ran / 4096; 00403946 shr …
sharptooth
  • 167,383
  • 100
  • 513
  • 979
44
votes
2 answers

Why is __int128_t faster than long long on x86-64 GCC?

This is my test code: #include #include #include using namespace std; using ll = long long; int main() { __int128_t a, b; ll x, y; a = rand() + 10000000; b = rand() % 50000; auto t0 =…
xxhxx
  • 871
  • 5
  • 11
41
votes
3 answers

How to divide two Int a get a BigDecimal in Kotlin?

I want to divide two Integers and get a BigDecimal back in Kotlin. E.g. 3/6 = 0.500000. I've tried some solutions, like: val num = BigDecimal(3.div(6)) println("%.6f".format(num)) // The result is: 0.000000 but none of them solve my problem.
Alif Al-Gibran
  • 1,166
  • 1
  • 13
  • 24
40
votes
6 answers

Why does dividing a float by an integer return 0.0?

So if I have a range of numbers '0 - 1024' and I want to bring them into '0 - 255', the maths would dictate to divide the input by the maximum the input will be (1024 in this case) which will give me a number between 0.0 - 1.0. then multiply that by…
Arif Driessen
  • 587
  • 2
  • 6
  • 8
38
votes
16 answers

Check if a number is divisible by 3

I need to find whether a number is divisible by 3 without using %, / or *. The hint given was to use atoi() function. Any idea how to do it?
josh
  • 13,793
  • 12
  • 49
  • 58
34
votes
4 answers

Why does division by 3 require a rightshift (and other oddities) on x86?

I have the following C/C++ function: unsigned div3(unsigned x) { return x / 3; } When compiled using clang 10 at -O3, this results in: div3(unsigned int): mov ecx, edi # tmp = x mov eax, 2863311531 # result =…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
33
votes
4 answers

Is integer division always equal to the floor of regular division?

For large quotients, integer division (//) doesn't seem to be necessarily equal to the floor of regular division (math.floor(a/b)). According to Python docs (https://docs.python.org/3/reference/expressions.html - 6.7), floor division of integers…
32
votes
4 answers

Why does integer division code give the wrong answer?

I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors: float res = quantity / standard; I have tried the above division with several values and I always get…
Julian C.
  • 339
  • 1
  • 3
  • 4
1
2
3
41 42