Questions tagged [floor-division]

48 questions
581
votes
16 answers

What is the difference between '/' and '//' when used for division?

Is there a benefit to using one over the other? In Python 2, they both seem to return the same results: >>> 6/3 2 >>> 6//3 2
Ray
  • 187,153
  • 97
  • 222
  • 204
65
votes
3 answers

Two forward slashes in Python

I came across this sample of code from a radix sort: def getDigit(num, base, digit_num): # pulls the selected digit return (num // base ** digit_num) % base What does the // do in Python?
Biff
  • 1,009
  • 3
  • 11
  • 20
18
votes
4 answers

Java - How to do floor division?

I know that in Python you can do floor division like this: 5 // 2 #2 The // is used for something totally different in Java. Is there any way to do floor division in Java?
user10654536
13
votes
1 answer

Weird result of floor division in numpy

I stumbled upon a floor division result of a np.float32 or np.float64 , which I do not understand I'm using numpy 1.15.4 in Python 3.6.7 >>> import numpy as np >>> np.float32(0)//1 -0.0 I know, that i can workaround by abs() and such, but why am I…
Michal
  • 163
  • 1
  • 7
11
votes
3 answers

What does the "variable //= a value" syntax mean in Python?

I came across with the code syntax d //= 2 where d is a variable. This is not a part of any loop, I don't quite get the expression. Can anybody enlighten me please?
Chris
  • 767
  • 1
  • 8
  • 23
9
votes
1 answer

How is floor division not giving result according to the documented rule?

>>> print (12//0.2) 59.0 >>> print(floor(12/0.2)) 60 Why floor division is not working according to the rule in this case? p.s. Here Python is treating 0.2 as 0.20000000001 in the floor division case So (12/0.2000000001) is resulting in…
9
votes
1 answer

Weird behaviour of division in python

I'm trying to solve this problem in hackerrank. At some point I have to check if a number divides n(given input) or not. This code works perfectly well except one test case(not an issue): if __name__ == '__main__': tc = int(input().strip()) …
Bharat
  • 1,044
  • 15
  • 34
9
votes
5 answers

Fast floor of a signed integer division in C / C++

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) { /* avoid modulo, use multiply instead */ …
ideasman42
  • 42,413
  • 44
  • 197
  • 320
4
votes
0 answers

What is the value of 5.5//1.1 in python? and How?

What's the step by step answer for 5.5//1.1. While running in compiler it gives 4.0 as an answer.
3
votes
2 answers

How to divide a 128-bit dividend by a 64-bit divisor, where the dividend's bits are all 1's, and where I only need the 64 LSBs of the quotient?

I need to calculate (2128 - 1) / x. The divisor, x, is an unsigned 64-bit number. The dividend is composed of two unsigned 64-bit numbers (high and low), where both numbers are UINT64_MAX. I can only use 64-bit arithmetic and need it to be portable…
tgonzalez89
  • 621
  • 1
  • 6
  • 26
3
votes
2 answers

Floor division // vs int() rounded off

I am a new user to Python 3.6.0. I am trying to divide 2 numbers that produces a big output. However, using return ans1 // ans2 produces 55347740058143507128 while using return int(ans1 / ans2) produces 55347740058143506432. Which is more accurate…
GordonJun
  • 35
  • 4
2
votes
0 answers

Why does numpy floor_divide with infinity differ from true_divide

numpy's floor division seems counterintuitive when dealing with np.inf. I feel like the answer should be same as true division in these examples >>> -7 // np.inf -1.0 >>> -7 / np.inf -0.0 >>> np.inf // 7 nan >>> np.inf / 7 inf In my head, an…
2
votes
0 answers

np.floor_divide with 0 as denominator - no error on Mac

np.floor_divide(1.0,0) results inf with RuntimeWarning: divide by zero encountered in floor_divide np.floor_divide(1.0,0) on Linux but results nan on Mac with no warning/error. Why this difference on Mac and Linux ?
Noushadali
  • 41
  • 5
2
votes
0 answers

Python Double Forward Slash Difference from Floor

I'm using Python3 and getting different results when using // versus math.floor (other than return types). My problem: print('Using //: ', 1.0 // 0.2) print('Using math.floor: ', math.floor(1.0 / 0.2)) Output: Using //: 4.0 Using math.floor: …
John_335
  • 21
  • 5
2
votes
1 answer

How to compute trunc(a/b) with only the nearest-to-even rounding mode?

Given two IEEE-754 double-precision floating-point numbers a and b, I want to get the exact quotient a/b rounded to an integer towards zero. A C99 program to do that could look like this: #include #include #pragma STDC FENV_ACCESS…
Řrřola
  • 6,007
  • 1
  • 17
  • 10
1
2 3 4