fmod()

Module of input numbers
fmod(x,y)
Return value is modulus of two input numbers with sign of x. Data type of return value is float.

Example
print(math.fmod(10,2)) # 0.0
print(math.fmod(10,3)) # 1.0
print(math.fmod(10,4)) # 2.0
Output will carry sign of x
print(math.fmod(10,-2))   # 0.0
print(math.fmod(10,-3))   # 1.0
print(math.fmod(10,4))    # 2.0
print(math.fmod(-10,4))   # -2.0
x is float here
print(math.fmod(10.34,4)) # 2.34
print(math.fmod(10,0))    # ValueError: math domain error
Using for loop
for i in range(0,10):
    print("fmod(",i,",2) = ",math.fmod(i,2))
Output
fmod( 0 ,2) =  0.0
fmod( 1 ,2) =  1.0
fmod( 2 ,2) =  0.0
fmod( 3 ,2) =  1.0
fmod( 4 ,2) =  0.0
fmod( 5 ,2) =  1.0
fmod( 6 ,2) =  0.0
fmod( 7 ,2) =  1.0
fmod( 8 ,2) =  0.0
fmod( 9 ,2) =  1.0
fmod() is generally preferred when working with floats ( data type ) , while Python’s x % y is preferred when working with integers.
Python modulo operator always return the remainder having the same sign as the divisor.
Watch the output and think why they are different?
Example 1
import math
x=-25
y=3
print(math.fmod(x,y))  # output -1.0
print(x % y)           # output 2
Example 2
import math
x=25
y=-3
print(math.fmod(x,y)) # output 1.0
print(x % y)          # output -2

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer