Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Floor Division and Remainder at same time in 2 separate variables

Is there a python built-in (or just optimized) function to get the floor division and remainder at the same time in two separate variables?

Example:

a, b = 10 divided by 4

Desired results:

a = 2
b = 2

I need this to be an optimized solution.

Performance results:

First piece of code:

for i in range(10000000):
    a, b = divmod(i, 5)
took 3.99 seconds to run

Second piece of code:

for i in range(10000000):
    a = i // 5
    b = i % 5
took 2.56 seconds to run

Remarks:

Read @casevh answer for a more detailed explanation.

tldr: divmod() works better if numbers are big.

like image 300
joaoavf Avatar asked Nov 22 '17 12:11

joaoavf


People also ask

How do you get both quotient and remainder in Python?

Get quotient and remainder with divmod() in Python In Python, you can calculate the quotient with // and the remainder with % . The built-in function divmod() is useful when you want both the quotient and remainder. divmod(a, b) returns a tuple (a // b, a % b) .

Is floor division same as division?

Floor division is a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result. Floor function is mathematically denoted by this ⌊ ⌋ symbol.

When using the floor division operator (/) if the result is negative then the result is rounded off to the next largest integer?

Floor division will also round down to the next lowest number, not the next lowest absolute value. 6 // 4 = 1.5 , which rounds down to 1, and up to 2. -6 // 4 = -1.5 , which rounds down to -2, and up to -1.

How to do floor division of two numbers in Python?

The floor () function of the math module returns the floor division of two integers. For example: As you can see clearly from the output, the floor () function returns the same result as the floor division operator ( // ). It’s also true for the negative numbers: Python uses // as the floor division operator and % as the modulo operator.

What is the difference between remainder and floor?

FLOOR is defined as "the largest integer number smaller than the parameter", thus: negative numbers: FLOOR (X)=integer part of X minus 1 (because it must be SMALLER than the parameter, i.e., more negative!) REMAINDER is defined as the "left over" of a division (Euclidean arithmetic).

What is the floor division of a number?

To understand the floor division, you first need to understand the floor of a real number. The floor of a real number is the largest integer less than or equal to the number. In other words: For example, the floor of 3.4 is 3 because 3 is the largest integer less than or equal to 3.4. The floor of 3.9 is also 3.

Why use floored division instead of Divmod?

Maybe, there are mathematical or performance reasons for using floored division, too. Note that usually, divmod exists because it is performs twice as fast as computing the two operations separately. Providing such a function without this performance benefit might be confusing.


1 Answers

Use this. this will help you.

a,b = divmod(10,2)

it will return both value

like image 147
Manoj Jadhav Avatar answered Sep 27 '22 17:09

Manoj Jadhav