Open In App

Benefits of Double Division Operator over Single Division Operator in Python

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. 

Python3




# A Python program to demonstrate use of 
# "//" for both integers and floating points
 
print(5//2)
print(-5//2)
print(5.0//2)


Output:

2
-3
2.0

The time complexity of the program is O(1) as it contains only constant-time operations.

The auxiliary space used by the program is O(1) as it only uses a fixed amount of memory to store the variables and constant values used in the program.

The single division operator behaves abnormally generally for very large numbers. Consider the following example. Examples 1: 

Python3




# single division
print(1000000002/2)
 
# Gives wrong output
print(int(((10 ** 17) + 2)/2))
 
# Gives Correct output
print(((10 ** 17) + 2)//2)


Output:

500000001.0
50000000000000000
50000000000000001

The time complexity of all three operations is constant time O(1) as they involve simple arithmetic operations.

The auxiliary space complexity of all three operations is also constant space O(1) as they do not use any extra memory that depends on the input size.

Example 2: 

Python3




x = 10000000000000000000006
if int(x / 2) == x // 2:
    print("Hello")
else:
    print("World")


Output: 

World

The Output should have been Hello if the single division operator behaved normally because 2 properly divides x. But the output is World because The results after Single Division Operator and Double Division Operator ARE NOT THE SAME. This fact can be used for programs such as finding the sum of first n numbers for a large n. 

Python3




n = 10000000000
 
s1 = int(n * (n + 1) / 2)
s2 = n * (n + 1) // 2
 
print("Sum using single division operator : ", s1)
print("Sum using double division operator : ", s2)


Output: 

Sum using single division operator :  50000000005000003584
Sum using double division operator :  50000000005000000000

Thus the result found by using the single division operator is Wrong, while the result found by using the double division operator is Correct. This is a huge benefit of Double Division Operator over Single Division Operator in Python.

The time complexity of the code is O(1), as the code involves only simple arithmetic operations.

The auxiliary space used by the code is O(1), as the code does not involve any data structures whose size depends on the input size.



Similar Reads

Single and Double Quotes | Python
Python string functions are very popular. There are two ways to represent strings in python. String is enclosed either with single quotes or double quotes. Both the ways (single or double quotes) are correct depending upon the requirement. Sometimes we have to use quotes (single or double quotes) together in the same string, in such cases, we use s
3 min read
Single Vs Double Quotes in Python Json
JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for data storage and exchange between web servers and clients. In Python, dealing with JSON is a common task, and one of the decisions developers face is whether to use single or double quotes for string representation within JSON objects. In this article, we'll
3 min read
Single Aad Double Underscores in Python
In Python, naming conventions play a crucial role in code readability and maintainability. Single and double underscores, when used in names, convey specific meanings and conventions. These naming conventions are widely adopted in the Python community and are often utilized in various contexts, including class attributes and special methods. In thi
4 min read
Parsing JSON with Single and Double Quote in Python
We are given JSON data and we will need to parse JSON data with a mix of single and double quotes. In this article, we will see how to parse JSON data with a mix of single and double quotes. Example: Input : '{"name": "John", "age": 30, "city": "New York"}' Output : John 30 New York Explanation : Here, we have JSON data with mix doublw and single q
3 min read
Count of index subsets such that maximum of values over these indices in A is at least total sum over B
Given two arrays A[] and B[] consisting of N positive integers, the task is to find the total number of subsets of indices such that the maximum value in the array A[] over all these indices is greater than or equal to the sum of all values over these indices in the array B[]. Examples: Input: A[] = {3, 1}, B[] = {1, 2}Output: 2Explanation:The tota
11 min read
Check the equality of integer division and math.floor() of Regular division in Python
For large quotients, floor division (//) doesn't seem to be necessarily equal to the floor of regular division (math.floor(a/b)) Examples: Input : 269792703866060742 // 3 Output : 89930901288686914 Input : math.floor(269792703866060742 / 3) Output : 89930901288686912 In the above examples, the output for floor division(//) is 89930901288686914 and
2 min read
Benefits of Heap over Sorted Arrays
Array: An array is a collection of similar data types which is stored in contiguous memory locations. Arrays are static data structures with limited size. The elements stored in the arrays are accessed by their unique indices. The array combines the data of similar types. When the elements within the array are sorted in an order then the array is c
2 min read
Difference between Single Precision and Double Precision
According to IEEE standard, floating-point number is represented in two ways: PrecisionBaseSignExponentSignificandSingle precision21823+1Double precision211152+1 1. Single Precision: Single Precision is a format proposed by IEEE for the representation of floating-point numbers. It occupies 32 bits in computer memory. 2. Double Precision: Double Pre
2 min read
Difference between Single Bus Structure and Double Bus Structure
1. Single Bus Structure: In a single bus structure, one common bus is used to communicate between peripherals and microprocessors. It has disadvantages due to the use of one common bus. 2. Double Bus Structure: In a double bus structure, one bus is used to fetch instructions while other is used to fetch data, required for execution. It is to overco
2 min read
Difference Between Single and Double Quotes in Shell Script and Linux
Single quotes and double quotes are both functional in Linux while working with shell scripts or executing commands directly in the terminal but there is a difference between the way the bash shell interprets them. Single quotes: Enclosing characters in single quotation marks (') holds onto the literal value of each character within the quotes. In
3 min read