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

Different results for idiv instruction

Take a look at this piece of code int main() { int i = 1U << 31; // assume this yields INT_MIN volatile int x; x = -1; x = i / x; //dividing INT_MIN by -1 is UB return 0; } It invokes undefined behavior on typical platform, but…
ntysdd
  • 1,206
  • 2
  • 9
  • 19
13
votes
4 answers

How can I perform division in a program, digit by digit?

I'm messing around with writing a class similar to mpz (C) or BigInteger (Java). This is just for fun, so please don't go on about how I shouldn't be writing my own. I have a class similar to: public class HugeInt { public List
Mithrax
  • 7,603
  • 18
  • 55
  • 60
12
votes
5 answers

128-bit division intrinsic in Visual C++

I'm wondering if there really is no 128-bit division intrinsic function in Visual C++? There is a 64x64=128 bit multiplication intrinsic function called _umul128(), which nicely matches the MUL x64 assembler instruction. Naturally, I assumed there…
cxxl
  • 4,939
  • 3
  • 31
  • 52
12
votes
1 answer

speed of elementary mathematical operations in Numpy/Python: why is integer division slowest?

EDIT2: As @ShadowRanger pointed out, this is a Numpy phenomenon, not Python. But when doing the calculations in Python with list comprehensions (so x+y becomes [a+b for a,b in zip(x,y)]) then all arithmetic operations still take equally long…
PDiracDelta
  • 2,348
  • 5
  • 21
  • 43
12
votes
4 answers

How to compute 2⁶⁴/n in C?

How to compute the integer division, 264/n? Assuming: unsigned long is 64-bit We use a 64-bit CPU 1 < n < 264 If we do 18446744073709551616ul / n, we get warning: integer constant is too large for its type at compile time. This is because we…
Wu Yongzheng
  • 1,707
  • 17
  • 23
12
votes
2 answers

How can I perform 64-bit division with a 32-bit divide instruction?

This is (AFAIK) a specific question within this general topic. Here's the situation: I have an embedded system (a video game console) based on a 32-bit RISC microcontroller (a variant of NEC's V810). I want to write a fixed-point math library. I…
12
votes
11 answers

Get percent of number in c++

How can I calculate several percent of int? for example I want to get 30% from number, if I will use this example of code, I will get wrong answer: int number = 250; int result = (number / 100) * 30; result will be 60, and the real answer is…
user1544067
  • 1,676
  • 2
  • 19
  • 30
12
votes
2 answers

Fast Division on GCC/ARM

As far as I know most compilers will do fast division by multiplying and then bit shifting to the right. For instance, if you check this SO thread it says that when you ask the Microsoft compiler to do division by 10 it will multiply the dividend by…
Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78
11
votes
2 answers

How would you perform ceiling division?

Avoiding a conversion over to floating point is necessary.
11
votes
2 answers

Division of two numbers in NASM

Starting to teach myself assembly (NASM) I wanted to know how to divide 2 numbers (For instance on Windows). My code looks like this but it crashes. global _main extern _printf section .text _main: mov eax, 250 mov ebx, 25 div ebx push ebx push…
Karen
  • 111
  • 1
  • 1
  • 3
11
votes
3 answers

Is it safe to replace "a/(b*c)" with "a/b/c" when using integer-division?

Is it safe to replace a/(b*c) with a/b/c when using integer-division on positive integers a,b,c, or am I at risk losing information? I did some random tests and couldn't find an example of a/(b*c) != a/b/c, so I'm pretty sure it's safe but not quite…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
11
votes
7 answers

Objective-C Integer Arithmetic

I'm trying to calculate some numbers in an iPhone application. int i = 12; int o = (60 / (i * 50)) * 1000; I would expect o to be 100 (that's milliseconds) in this example but it equals 0 as displayed by NSLog(@"%d", o). This also equals 0. int o =…
Stateful
  • 737
  • 2
  • 9
  • 25
11
votes
3 answers

What does the "variable //= a value" syntax mean in Python?

I came across with the code syntax d //= 2 where d is a variable. This is not a part of any loop, I don't quite get the expression. Can anybody enlighten me please?
Chris
  • 767
  • 1
  • 8
  • 23
11
votes
3 answers

Why does a division result differ based on the cast type?

Here's a part of code that I dont understand: byte b1 = (byte)(64 / 0.8f); // b1 is 79 int b2 = (int)(64 / 0.8f); // b2 is 79 float fl = (64 / 0.8f); // fl is 80 Why are the first two calculations off by one? How should I perform this operation, so…
sydd
  • 1,824
  • 2
  • 30
  • 54
10
votes
1 answer

The integer division algorithm of Intel's x86 processors

Which integer division algorithm does Intel implement in their x86 processors?
n0p
  • 713
  • 10
  • 23