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

Integer division: is a//b == int(a/b) true for all integers a,b?

I know that integer division will always return the same answer as truncation of a floating point result if the numbers are both positive. Is it true if one or both of them are negative? I was just curious to know if there was an integer division…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
6
votes
4 answers

Divide the number into random number of random elements?

If I need to divide for example 7 into random number of elements of random size, how would I do this? So that sometimes I would get [3,4], sometimes [2,3,1] and sometimes [2,2,1,1,0,1]? I guess it's quite simple, but I can't seem to get the…
Stpn
  • 6,202
  • 7
  • 47
  • 94
5
votes
2 answers

Quick alternatives to floating point multiplication to calculate percentages

I'm writing some code on an Arduino that needs to run fast and make rough approximations to percentages of integers. For example, given a number I want to find 90% of it, or 70% or 30% etc. The obvious way to do it is multiply by a floating point…
interstar
  • 26,048
  • 36
  • 112
  • 180
5
votes
0 answers

Computing 2¹²⁸ % uint64_t

How to efficiently compute 2¹²⁸ % n, where n is uint64_t (and non-zero)? If I had access to a narrowing division like _udiv128 intrinsic of MSVC I could do: uint64_t remainder; _udiv128(1, 0, n, &remainder); _udiv128(remainder, 0, n,…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
5
votes
5 answers

Is there a c++ function (built-in or otherwise) that gives the integer division and modular division results without repeating the operation?

You could write something like: int i = 3; int k = 2; int division = i / k; int remainder = i % k; It seems as thought this would, on a low level, ask an ALU to perform two vision operations: one returning the quotient, and one returning the…
Adam S
  • 8,945
  • 17
  • 67
  • 103
5
votes
4 answers

Fast integer division and modulo with a const runtime divisor

int n_attrs = some_input_from_other_function() // [2..5000] vector corr_indexes; // size = n_attrs * n_attrs vector selected; // szie = n_attrs vector> selectedPairs; // size = n_attrs / 2 // vector::reserve everything…
Huy Le
  • 1,439
  • 4
  • 19
5
votes
2 answers

How to quickly divide a big uinteger into a word?

I am currently developing a class to work with big unsigned integers. However, I need incomplete functionality, namely: bi_uint+=bi_uint - Already implemented. No complaints. bi_uint*=std::uint_fast64_t - Already implemented. No…
PavelDev
  • 579
  • 4
  • 12
5
votes
1 answer

Solve "integer division in floating-point context" warning

How does one solve the "integer division in floating-point context" warning, in this line of code: int fps = 60; double timePerTick = 1000000000 / fps;
user13842198
5
votes
3 answers

Changing python math module behaviour for non-positive numbers division

Non-positive number division is quite different in c++ and python programming langugages: //c++: 11 / 3 = 3 11 % 3 = 2 (-11) / 3 = -3 (-11) % 3 = -2 11 / (-3) = -3 11 % (-3) = 2 (-11) / (-3) = 3 (-11) % (-3) = -2 So, as you can see, c++ is…
karlicoss
  • 2,501
  • 4
  • 25
  • 29
5
votes
2 answers

Why do integer div and mod round towards zero?

Unlike in C, in Java is the result of x/y and x%y well-defined even for negative operands. Surprisingly, it's defined by rounding towards zero, and not by rounding down (i.e., towards negative infinity). Does anybody have taken any advantage of this…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
5
votes
2 answers

How can I instruct the MSVC compiler to use a 64bit/32bit division instead of the slower 128bit/64bit division?

How can I tell the MSVC compiler to use the 64bit/32bit division operation to compute the result of the following function for the x86-64 target: #include uint32_t ScaledDiv(uint32_t a, uint32_t b) { if (a > b) return…
5
votes
1 answer

Displaying numbers with DOS

I was tasked to write a program that displays the linear address of my program's PSP. I wrote the following: ORG 256 mov dx,Msg mov ah,09h ;DOS.WriteStringToStandardOutput int 21h …
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
5
votes
7 answers

how to calculate (a times b) divided by c only using 32-bit integer types even if a times b would not fit such a type

Consider the following as a reference implementation: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint64_t x = a; x = x * b; x = x / c; return x; } I am interested in an implementation (in C or…
5
votes
1 answer

Difference in long int division in python 3

I came across a bizarre situation while doing some large number division in python. int(1012337203685477580 / 2) = 506168601842738816 and int(1012337203685477580 >> 1) = 506168601842738790 Why is there a difference between the two approaches?…
sandeeps
  • 966
  • 2
  • 9
  • 11
5
votes
3 answers

Python 2.7 - Continued Fraction Expansion - Understanding the error

I've written this code to calculate the continued fraction expansion of a rational number N using the Euclidean algorithm: from __future__ import division def contFract(N): while True: yield N//1 f = N - (N//1) if f ==…