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

Pair of points with distance divisible by integer

I came across an interview questions and despite the fact I've been trying to solve it on my own I think I need some help. I've got an array of integer numbers (positive and negative) representing points in space, the distance between two points is…
Fra
  • 176
  • 2
  • 7
5
votes
4 answers

Haskell Novice Trouble with Splitting a List in Half

Here's my attempt to write a function that splits a list of even length into two equal halves. halve :: [a] -> ([a], [a]) halve x | even len = (take half x, drop half x) | otherwise = error "Cannnot halve a list of odd length" where …
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
5
votes
1 answer

fixed point integer division ("fractional division") algorithm

The Honeywell DPS8 computer (and others) have/had a "divide fractional" instruction: "This instruction divides a 71-bit fractional dividend (including sign) by a 36-bit fractional divisor (including sign) to form a 36-bit fractional quotient…
user1538392
  • 81
  • 1
  • 5
5
votes
5 answers

Error with division using double type in Java

Okay. I have been bashing my head against the wall for like 2 hours now trying to figure out why in the world double answer = 364/365; is telling me that answer is 0. Or any other combination of double for that matter, its just truncating the…
Jeff
  • 97
  • 1
  • 1
  • 5
5
votes
2 answers

Python 3 strange division

About half an hour thinking "what am i doing wrong!?" on the 5-lines code.. because Python3 is somehow rounding big integers. Anyone know why there is a problem such: Python2: int(6366805760909027985741435139224001 # This is 7**40. / 7)…
inexxt
  • 81
  • 1
  • 3
4
votes
2 answers

Why in Ruby 0.0/0, 3.0/0 and 3/0 behave differently?

If I divide by 0, I get either a ZeroDivisionError, Infinity or NaN depending on what is divided. ruby-1.9.2-p180 :018 > 0.0 / 0 => NaN ruby-1.9.2-p180 :020 > 3.0 / 0 => Infinity ruby-1.9.2-p180 :021 > 3 / 0 ZeroDivisionError: divided by 0 I…
Evgeny Shadchnev
  • 7,320
  • 4
  • 27
  • 30
4
votes
1 answer

Why does gcc 12.2 not optimise divisions into shifts in this constexpr function called from main()

I have been playing around with the Godbolt Compiler and typed this code: constexpr int func(int x) { return x > 3 ? x * 2 : (x < -4 ? x - 4 : x / 2); } int main(int argc) { return func(argc); } The code is somewhat straight forward. The…
4
votes
2 answers

Is it possible to implement the extended euclidean algorithm with unsigned machine words?

I'm trying to find a way to implement EEA using uint64_t in C, on a system that will not support 128-bit integers. The problem is that it seems like there is always a case where some variable or another will overflow, generating incorrect…
BadZen
  • 4,083
  • 2
  • 25
  • 48
4
votes
3 answers

Why is ARM gcc calling __udivsi3 when dividing by a constant?

I'm using the latest available version of ARM-packaged GCC: arm-none-eabi-gcc (GNU Arm Embedded Toolchain 10-2020-q4-major) 10.2.1 20201103 (release) Copyright (C) 2020 Free Software Foundation, Inc. When I compile this code using "-mcpu=cortex-m0…
Dan Sandberg
  • 549
  • 1
  • 4
  • 15
4
votes
2 answers

Why is it necessary to add a bias to the dividend for signed division by a power of 2?

I'm learning about assembly code in "Computer Systems- A Programmer's Perspective", and I encountered the following example: In the following C function, we have left the definition of operation OP incomplete: #define OP /* Unknown operator…
Richie Thomas
  • 3,073
  • 4
  • 32
  • 55
4
votes
2 answers

Why is /=2 different from >>=1 for signed integers, and compiles to different asm?

unsigned int a=200; //mov dword ptr [a],0C8h a >>= 1; //mov eax,dword ptr [a] //shr eax,1 //mov dword ptr [a],eax a /= 2; //mov eax,dword ptr [a] //shr eax,1 //mov dword ptr [a],eax int b = -200; //mov dword ptr…
sunkue
  • 278
  • 1
  • 9
4
votes
1 answer

Consistent integer division with floating point operations

I need to do integer division in JavaScript, which only gives me double-precision floating point numbers to work with. Normally I would just do Math.floor(a / b) (or a / b | 0) and be done with it, but in this case I'm doing simulation executed in…
A.L.
  • 760
  • 5
  • 13
4
votes
5 answers

Which is the best way, in C, to see if a number is divisible by another?

Which is the best way, in C, to see if a number is divisible by another? I use this: if (!(a % x)) { // this will be executed if a is divisible by x } Is there anyway which is faster? I know that doing, i.e, 130 % 13 will result into doing 130 / 13…
Joseph
  • 401
  • 2
  • 6
  • 10
4
votes
1 answer

Is there a way to optimize this code for finding the divisors of a number?

I've written a program in Julia to compute the divisors of a number n efficiently. The algorithm is original (as far as I know), and is loosely based on the Sieve of Eratosthenes. It essentially works like this: For a given prime p, let p^k || n;…
4
votes
1 answer

How to design a Turing machine for division of two numbers?

The machine takes 2 natural numbers (a, b) as input in unary form and outputs the integer quotient and the remainder of the integer division a / b. What would the initial and final state on the tape be? What would the functionality diagram look…