How to perform modulo with negative values in Python?
Last Updated :
21 Aug, 2024
Taking the modulo of a negative number is a bit more complex mathematics which is done behind the program of Python. If we don't understand the mathematics behind the modulo of negative numbers then it will become a huge blunder.
Mathematics behind the negative modulo :
Let's Consider an example, where we want to find the -5mod4 i.e. -5%4. You all must be wondering that the answer will be according to the below rule -
-5/4 = -1.25
and
math.floor(-1.25) = -2
But this is not the answer we get, when we'll run the above code we'll get the answer as 3. This is because Python's modulo operator (%) always returns a number having the same sign as the denominator. What happens behind the scene is that Python applies the distribute law of Modulo operator which is -
(a+b)mod n = [(a mod n)+(b mod n)]mod n
To apply this math Python break the given statement as -
-5%4 = (-2*4 + 3) % 4 = 3
This was done so that the (-2*4)%4 will give the answer as 0 (as we are always getting a multiple of divisor as our first number) and the overall result will be 3. Let's see more examples for better understanding.
Examples:
-3 % 7 = ( -1*7 + 4 ) % 7 = 4
-5 % 2 = (-3*2 + 1) % 2 = 1
Example #1 :
In this example, we can see that by using this mathematics, we are able to perform and understand the negative modulo.
Python3
# Using negative modulo
res1 = -5 % 4
res2 = ((-2*4) + 3) % 4
print(res1)
print(res2)
Output :
3
3
Example #2 :
Python3
# Using negative modulo
res1 = -3 % 7
res2 = - 12 % 4
print(res1)
print(res2)
Output :
4
0
Approach:
Import the math module at the beginning of your code. This module provides access to mathematical functions, including fmod().
Define the values of x and y that you want to perform modulo on. In this example, x is -10 and y is 3.
Call the math.fmod() function, passing in x and y as arguments. This function returns the remainder of dividing x by y, with the sign of x.
Print the result to the console to verify that the function worked correctly.
Python
import math
x = -10
y = 3
result = math.fmod(x, y)
print(result) # Output: -1.0
Time complexity:
The time complexity of the math.fmod() function is O(1), which means that it takes a constant amount of time to execute regardless of the size of the input values.
Space complexity:
The auxiliary space of the math.fmod() function is also O(1), as it only requires a small amount of memory to store the input values and the result.
Similar Reads
Slicing with Negative Numbers in Python Slicing is an essential concept in Python, it allows programmers to access parts of sequences such as strings, lists, and tuples. In this article, we will learn how to perform slicing with negative indexing in Python.Indexing in PythonIn the world of programming, indexing starts at 0, and Python als
5 min read
Compute the square root of negative input with emath in Python In this article, we will cover how to compute the square root of negative inputs with emath in Python using NumPy. Example:Input: [-3,-4] Output: [0.+1.73205081j 0.+2.j ] Explanation: Square root of a negative input.NumPy.emath.sqrt method: The np.emath.sqrt() method from the NumPy library calculate
2 min read
Modulo operator (%) in Python Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/
4 min read
How to Drop Negative Values in Pandas DataFrame Handling data effectively is crucial in data analysis and manipulation. One common task is cleaning the data by removing negative values, especially when they are considered outliers or invalid entries. The Pandas library in Python offers several efficient methods to accomplish this. This article wi
3 min read
How to Round Numbers in Python? Rounding a number simplifies it while keeping its value as close as possible to the original. Python provides various methods to round numbers, depending on how we want to handle the precision or rounding behavior. In this article, we'll cover the most commonly used techniques for rounding numbers i
4 min read