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

How to multiply a 64 bit integer by a fraction in C++ while minimizing error?

Given a 64 bit (signed) long long or __int64, how would you go about multiplying it by an arbitrary fraction while at the same time minimizing error? Three simple sketches: int64_t numerator = ...; int64_t denominator = ...; int64_t x = ...; // a,…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
4
votes
4 answers

Why is -1/2 evaluated to 0 in C++, but -1 in Python?

C++: cout << -1/2 evaluates to 0 Python: -1/2 evaluates to -1. Why is this the case?
Chao Xu
  • 41
  • 3
4
votes
1 answer

Integer division and bit shift in JavaScript

Does the JavaScript code var n = 8; // or some arbitrary integer literal n >> 1; always denote "integer devision by 2 without remainer"? My concern is the endianess if the integer literal is larger than one byte. The background of my question is…
user2690527
  • 1,729
  • 1
  • 22
  • 38
4
votes
2 answers

Is INT_MIN/-1 defined behavior in C++?

I have the following code for INT_MIN/-1. I would expect for this to have came out to be INT_MAX+1 (or 0 with rollover). However, the actual result I get is INT_MIN. This is my test code: #define __STDC_LIMIT_MACROS #include #include…
Earlz
  • 62,085
  • 98
  • 303
  • 499
4
votes
6 answers

Why is the output of cout << 7/9*9; is zero?

Why is the output of the following code equals to 0 or serven? cout << 7/9*9; //output 0 (zero) why? float nine = 9; float seven = 7; float i = seven/nine*nine; cout << i //output 7 Why? Thanks for the help.
user186757
4
votes
1 answer

Dividing negative numbers with floordiv in python

I'm confused by the nature of integer division with // or floordiv with negative numbers in python. >>> -5 // 2 -3 >>> int(-5/2) -2 Why would floordiv() round down to -3? I thought integer division was supposed to simply drop (or lack) the…
Kevin Lee
  • 89
  • 1
  • 6
4
votes
6 answers

How do I compute the first decimal digit of a division between large numbers?

I have two unsigned long longs X and Y, where X < Y, but both may be very large. I want to compute the first digit past the decimal point of X / Y. For example, if X is 11 and Y is 14, then 11 / 14 is .785…, so the result should be 7. (X * 10) / Y…
user123029
  • 41
  • 3
4
votes
1 answer

Java Division error

I have the following variables: int first = 0; int end = 0; Declare in the public class. Within a method: double diff = end / first; double finaldiff = 1 - diff; The end variable on System.out.println is 527, the first is 480. Why is the answer…
user860511
3
votes
2 answers

Integer division in PHP produces zero - how to cast it to float?

In a Russian card game I'm trying to keep statistics of how often a player swears (says "bad words" - and we do have a lot of them in Russian language) in the following PostgreSQL table: # select * from pref_chat order by swear desc; id …
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
3
votes
5 answers

int64 doesn't support LanguagePrimitives.DivideByInt?

Why int64 doesn't support LanguagePrimitives.DivideByInt? I thought it would be pretty natural to write something like this: let inline DivBy2 n = LanguagePrimitives.DivideByInt n 2 let res = DivBy2 100L But compiler says that int64 doesn't support…
Dmitrii Lobanov
  • 4,897
  • 1
  • 33
  • 50
3
votes
1 answer

Does do_div() in Linux work in 32 and 64 bit architectures?

I need to do an integer division in a kernel module and I am using do_div() for that. It seems to work on my machine (I have an i686 processor), however I am not sure that it works everywhere. Could anyone confirm whether do_div() should function…
Dani Camps
  • 195
  • 2
  • 5
3
votes
0 answers

How do I get the whole number when using "/" operator in python

Below is my code for a problem on leetcode: for i in range(len(nums)): ans =0 ans+=nums[i] for j in range(i+1,len(nums)): realans=0 ans+=nums[j] realans= ans/k if isinstance(realans,int): …
Tom Brooke
  • 171
  • 3
  • 15
3
votes
2 answers

Implementing hardware that divides an 8 bit number by 3 (11) in binary

I want to create a schematic that divides any 8-bit number by 3, on a Xilinx device in case that matters. For example, hardware takes two inputs (111101) and (11) and returns the division of two numbers which is 010100. I don't need to worry about…
user19076994
3
votes
1 answer

Why is there only little difference in integer division throughput with larger values on AMD Zen?

I was curious about the differences in performance when dividing two values with varying number of value-bits but constant operand (register) bits. I expected the performance to be dependent of the highest set bit of the dividend since I assume an…
3
votes
2 answers

Visual C++ generates DIV instead of IDIV (x86, integer arithmetic)

I'm working with Visual C++ 2008 here (9.x) and I was preparing a fixed point value when I ran into the compiler generating a DIV instead of an IDIV. I collapsed the code into a tiny piece to exactly reproduce: short a = -255; short divisor16 = 640;…
nielsj
  • 1,499
  • 12
  • 24