remainder()

remainder(x , y) Return remainder of x with respect to y.
remainder(n)  

Example
import math
print(math.remainder(12,7)) # -2.0
print(math.remainder(12,5)) #  2.0
import math
print(math.remainder(13,7))  # -1.0 
Using for loop
for i in range (1,15):
    print("math.remainder(13,{:2d})".format(i),math.remainder(13,i))
Output
math.remainder(13, 1) 0.0
math.remainder(13, 2) 1.0
math.remainder(13, 3) 1.0
math.remainder(13, 4) 1.0
math.remainder(13, 5) -2.0
math.remainder(13, 6) 1.0
math.remainder(13, 7) -1.0
math.remainder(13, 8) -3.0
math.remainder(13, 9) 4.0
math.remainder(13,10) 3.0
math.remainder(13,11) 2.0
math.remainder(13,12) 1.0
math.remainder(13,13) 0.0
math.remainder(13,14) -1.0
Example 1: Comparison between Modulus and Remainder
Description: The % operator always gives a non-negative remainder, whereas math.remainder() can return negative results depending on which value is closer to zero.
import math
# Modulus operation
print(12 % 7)  # Output: 5, because modulus returns positive remainder

# Remainder using math.remainder
print(math.remainder(12, 7))  # Output: -2.0, because it returns the remainder closest to zero
Example 2: Use Case: Angle Calculation in Range [-180, 180]
Description: math.remainder() is useful for angle normalization, ensuring values stay within a desired range.
import math
angle = 370  # Large angle value in degrees
adjusted_angle = math.remainder(angle, 360)
print(f"Adjusted angle: {adjusted_angle}°")  # Output: 10.0°
Output
Adjusted angle: 10.0°
Example 3: Use Case: Periodic Time Calculation
Description: Useful for handling cycles, such as time or periodic events, by calculating the remainder of time elapsed.
import math
hours_passed = 50
cycle = 24  # 24-hour clock
corrected_time = math.remainder(hours_passed, cycle)
print(f"Corrected time: {corrected_time}")  # Output: 2.0

Explanation:

math.remainder(): It returns the value closest to zero, so the remainder can be negative.
Modulus Operator (%): It always returns a non-negative result.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer