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

MATLAB division... should 29/128 return 0?

I really don't think this is a precision problem, the answer should be about 0.226. Here's the exact code: val = I(i,j) bucketSize pos = val / bucketSize I is just a matrix I'm taking values from. Here is the output from MATLAB: val = …
jakev
  • 2,815
  • 3
  • 26
  • 39
7
votes
6 answers

Integer vs floating division -> Who is responsible for providing the result?

I've been programming for a while in C++, but suddenly had a doubt and wanted to clarify with the Stackoverflow community. When an integer is divided by another integer, we all know the result is an integer and like wise, a float divided by float is…
nsivakr
  • 1,565
  • 2
  • 25
  • 46
7
votes
4 answers

Unsigned 128-bit division on 64-bit machine

I have a 128-bit number stored as 2 64-bit numbers ("Hi" and "Lo"). I need only to divide it by a 32-bit number. How could I do it, using the native 64-bit operations from CPU? (Please, note that I DO NOT need an arbitrary precision library. Just…
rookie
  • 205
  • 1
  • 3
  • 6
7
votes
5 answers

Division in C# to get exact value

If I divide 150 by 100, I should get 1.5. But I am getting 1.0 when I divided like I did below: double result = 150 / 100; Can anyone tell me how to get 1.5?
Irannabi
  • 129
  • 1
  • 4
  • 15
6
votes
5 answers

Integer division in Python

I'm confused about the following integer math in python: -7/3 = -3 since (-3)*3 = -9 < -7. I understand. 7/-3 = -3 I don't get how this is defined. (-3)*(-3) = 9 > 7. In my opinion, it should be -2, because (-3)*(-2) = 6 < 7. How does this work?
Jonathan
  • 95
  • 1
  • 5
6
votes
3 answers

Division not crossing over bytes

I'm trying to do division on a uint128_t that is made up of 2 uint64_ts. Weirdly enough, the function works for uint64_ts with only the lower value set and the upper value = 0. I don't understand why. Here's the code for the division and bit…
calccrypto
  • 8,583
  • 21
  • 68
  • 99
6
votes
5 answers

signed integer division with rounding in C

I'd like to calculate x/y where x and y are both signed integers, and get a result rounded to the nearest integer. Specifically, I'd like a function rquotient(x, y) using integer-only arithmetic such that: ASSERT(rquotient(59, 4) ==…
fearless_fool
  • 33,645
  • 23
  • 135
  • 217
6
votes
2 answers

Checkstyle rule for suspicious integer division?

Is there a checkstyle rule that will catch something like this: double result = someInt / someOtherInt; result is double (so clearly fractions are desired) yet the right-hand side would do integer division (rounding down). Does something like this…
radai
  • 23,949
  • 10
  • 71
  • 115
6
votes
2 answers

remainder of integer division by 0

Consider integer division a = bq + r where a, b, q, r are respectively: dividend, divisor, quotient, and remainder. Particularly when b = 0, there is no unique q that satisfies the equation for a given a, and hence it makes sense that the quotient…
sawa
  • 165,429
  • 45
  • 277
  • 381
6
votes
2 answers

"if" statement in c++ doesn't evaluate conditions from left to right

I was referring to the question ""IF" argument evaluation order?" for understanding the evaluation order for "if" statement in c++. Here is the code where the conditions in if statements are evaluated in a wrong order. #include using…
Ayush Pandey
  • 565
  • 6
  • 19
6
votes
3 answers

How can i convert Integer value to decimal value?

i have an Integer value: Integer value = 56472201; Where the value could be positive or negative. When I divide the value by 1000000, I want this result in the form 56.472201 but instead it gives me just the quotient. How am I able to get both the…
jimmy
  • 8,121
  • 11
  • 36
  • 40
6
votes
3 answers

division an integer into k parts

I'm programming in java and I need to formulate an algorithm. The requirements of the algorithm are: We have 3 Integer variables n, m, k; We want to divide n into k parts so that the sum of the k-parts are equal to n and each part is an integer…
6
votes
1 answer

Getting 1000 as an answer instead of 1 when dividing (6.022/6.022=1000) Java

So, I'm new to programming, and I was trying to make a basic mole (chemistry) calculator just for fun. I didn't find this question. If it was answered please send me the link. This is the formula: n = N / Na where n = mole and Na = 6.022E23 The…
EnderEgg
  • 315
  • 1
  • 5
  • 13
6
votes
4 answers

How to divide unsigned integer of 256 and round to the closer value?

I need to divide number of 256 and round it to closer value, for example, if input 255 I want to get 1, not 0. Now I'm using int x = 150; int z = MulDiv(x, 1, 256); But I think it is not optimal way to reach my goal, can someone advice better way.
user2779346
6
votes
2 answers

How to divide the integer value

I want to perform integer division in VB.NET, i.e. only keep the whole part of division result. Dim a, b, c as int32 a = 3500 b = 1200 c = a/b This example outputs 3. How do I make it return 2 instead?
Gopal
  • 11,712
  • 52
  • 154
  • 229