integer-division Questions

6

The modulo in Python is confusing. In Python, % operator is calculating the remainder: >>> 9 % 5 4 However: >>> -9 % 5 1 Why is the result 1? and not -4?
Avaunt asked 24/1, 2013 at 4:43

6

Solved

In C a floor division can be done, eg: int floor_div(int a, int b) { int d = a / b; if (a < 0 != b < 0) { /* negative output (check inputs since 'd' isn't floored) */ if (d * a != b) { /*...
Grabble asked 17/9, 2017 at 14:53

6

Solved

In Java, if we divide bytes, shorts or ints, we always get an int. If one of the operands is long, we'll get long. My question is - why does byte or short division not result in byte or short? Why...
Sauder asked 8/12, 2016 at 8:3

4

Solved

At some point in my program I compute an integer divisor d. From that point onward d is going to be constant. Later in the code I will divide by that d several times - performing an integer divisi...
Blaise asked 27/7, 2017 at 14:23

3

Solved

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 infi...
Slifka asked 15/4, 2011 at 21:39

1

Solved

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, &a...
Bramblett asked 12/1, 2023 at 12:32

3

Solved

How can I perform ceiling division on an integer? I want to avoid converting to a floating point value. For example, 7 / 4 should return 2, not 1.
Anissa asked 31/5, 2022 at 5:59

11

Solved

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.
Thespian asked 5/4, 2011 at 21:4

27

Solved

I was curious to know how I can round a number to the nearest whole number. For instance, if I had: int a = 59 / 4; which would be 14.75 if calculated in floating point; how can I store the resu...
Zettazeugma asked 11/3, 2010 at 5:20

9

Solved

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 d...

5

What would be the simplest formula to implement int divround(int a, int b) {...}, in C, where output is a/b with banker's rounding (round half to even)? For example, divround(3,2) and divround(5,2)...
Giffard asked 3/9, 2023 at 2:28

21

Solved

In JavaScript, how do I get: The whole number of times a given integer goes into another? The remainder?
Predicate asked 19/11, 2010 at 18:53

6

Solved

I remember in java that, the modulo operator could be inverted so that rather than seeing what the remainder is of an operation, you could invert it, so instead it will tell you many times a number...
Lupitalupo asked 14/7, 2012 at 22:7

7

Solved

Here is the code I'm using in the example: PRINT @set1 PRINT @set2 SET @weight= @set1 / @set2; PRINT @weight Here is the result: 47 638 0 I would like to know why it's returning 0 instea...
Norenenorfleet asked 3/11, 2009 at 10:12

4

Solved

int n_attrs = some_input_from_other_function() // [2..5000] vector<int> corr_indexes; // size = n_attrs * n_attrs vector<char> selected; // szie = n_attrs vector<pair<int,int>&...
Housework asked 8/9, 2022 at 9:36

5

Solved

I'm trying to solve this particular problem from PGExercises.com: https://www.pgexercises.com/questions/aggregates/rankmembers.html The gist of the question is that I'm given a table of club memb...
Gyrocompass asked 18/12, 2016 at 16:21

7

Solved

Is there is a simple, pythonic way of rounding to the nearest whole number without using floating point? I'd like to do the following but with integer arithmetic: skip = int(round(1.0 * total / su...
Doubt asked 16/10, 2010 at 19:15

2

Solved

I have the following function: pub fn s_v1(n: &u64) -> u64 { let mut x: u64 = 1; for i in 1..=*n { x = x * (*n + i) / i; } x } This code gives the correct answer for s_v1(&20) ==...
Cchaddie asked 9/8, 2022 at 4:49

4

Solved

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 ...
Seamanlike asked 8/12, 2009 at 22:2

5

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 su...
Chant asked 31/1, 2020 at 18:57

3

Solved

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...
Hepato asked 9/1, 2019 at 8:56

19

Solved

I was writing this code: public static void main(String[] args) { double g = 1 / 3; System.out.printf("%.2f", g); } The result is 0. Why is this, and how do I solve this problem?
Darill asked 13/1, 2011 at 21:25

1

For example, https://godbolt.org/z/W5GbYxo7o #include<cstdint> void divTest1(int * const __restrict__ val1, int * const __restrict__ val2, int * const __restrict__ val3) { for(int i=0;i<...
Schizophrenia asked 2/5, 2022 at 13:39

2

Solved

As quoted in "Integer division rounding with negatives in C++", in C before C99 (i.e. in C89) and in C++ before C++11 (i.e. in C++98 and C++03), for an integer division computation where either ope...
Pontificate asked 22/7, 2013 at 19:5

2

Solved

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_fa...
Pennypennyaliner asked 8/3, 2022 at 20:46

© 2022 - 2025 — McMap. All rights reserved.