remainder(n)
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 Remainderimport 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]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 Calculationimport 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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.