Playing around with some numbers may help:

from math import remainder, fmod

# in case x is a multiple of y
remainder(20, 4)    # 0.0
fmod(20, 4)         # 0.0

# in any other case
remainder(20, 3)    # -1.0
fmod(20, 3)         # 2.0

So what we see here is that the remainder, as stated in the docs, returns the difference between x and the closest integer multiple of y. As 20 lies between 18 and 21 (the 6- and 7-multiples of 3), it chooses whichever is closer to x. In this case it is 21, so the difference between x and 21 is -1.0.

In contrast, fmod returns the result of 20 mod 3, which is 2.0. I explicitly do not use the % operator, as the docs state that the result may differ.

Answer from DocDriven on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_math_fmod.asp
Python math.fmod() Method
The math.fmod() method returns the remainder (modulo) of x/y.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ math.html
math โ€” Mathematical functions
February 23, 2026 - In these cases, math.fma returns a NaN, and does not raise any exception. Added in version 3.13. ... Return the floating-point remainder of x / y, as defined by the platform C library function fmod(x, y).
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_math_fmod_method.htm
Python math.fmod() Method
The Python math.fmod() method is used to calculate the floating-point remainder of dividing one number by another. Mathematically, it calculates the remainder when dividing the first argument (dividend) by the second argument (divisor), where both
๐ŸŒ
Interactive Chaos
interactivechaos.com โ€บ en โ€บ python โ€บ function โ€บ mathfmod
math.fmod | Interactive Chaos
January 20, 2021 - The math.fmod function returns the modulus of two numbers as defined in the C platform library. This assumes that the modulus of two numbers a and b is defined as the result of a - n * b for some integer n such that the result has the same sign as a and a magnitude less than the absolute value ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-mathfmod-in-python
What is math.fmod() in python?
Python is a high level programming language that has a range of built-in modules and functions. The math module provides methods to perform mathematical operations in a straightforward manner. One operation is finding the modulo of two numbers, which is performed by math.fmod().
๐ŸŒ
Pythontic
pythontic.com โ€บ modules โ€บ math โ€บ fmod
The fmod() function - Python math module | Pythontic.com
The fmod() function from the math module of Python returns the floating point remainder of a division operation. The function is implemented as per the definition of fmod() function in the C language standard.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ math-fmod-method-with-example.aspx
math.fmod() method with example in Python
April 17, 2019 - math.fmod() method is a library method of math module, it is used to find the modulus or given numbers, where the first parameter is a dividend and the second parameter is divisor.
Find elsewhere
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-fmod
Python fmod Function
March 26, 2025 - The Python fmod function is a math function used to calculate the Module of the specified arguments. The syntax of Function is math.fmod(x, y)
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-math-fmod
Python math.fmod() - Modulus Function
Discover how to use the Python math.fmod() function to compute the floating-point remainder of division. This tutorial includes syntax, practical examples, and explanations of handling special cases like infinity and NaN.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-fmod-function
Python | fmod() function - GeeksforGeeks
February 20, 2023 - fmod() function is one of the Standard math library function in Python, which is used to calculate the Module of the specified given arguments.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ python modulo โ€“ % operator, math.fmod() examples
Python Modulo - % Operator, math.fmod() Examples - AskPython
February 16, 2023 - >>> import math >>> math.fmod(-5, 3) -2.0 >>> math.fmod(5, -3) 2.0 >>> math.fmod(-10, 3) -1.0 >>>
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ lua โ€บ mathematical library โ€บ fmod()
Lua | Mathematical Library | fmod() | Codecademy
October 26, 2023 - Includes 6 CoursesIncludes 6 Courses ... hours4 hours ยท The syntax of fmod() is as follows: math.fmod(num1, num2) Where num1 is the value to be divided by num2....
๐ŸŒ
Plus2Net
plus2net.com โ€บ python โ€บ math-fmod.php
fmod() to get Modulus of given input numbers in Python
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 ยท
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.fmod.html
numpy.fmod โ€” NumPy v2.2 Manual
This is the NumPy implementation of the C library function fmod, the remainder has the same sign as the dividend x1. It is equivalent to the Matlab(TM) rem function and should not be confused with the Python modulus operator x1 % x2.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ modulo-operator-python
Modulo Operator in Python: Understanding Key Concepts | DataCamp
March 12, 2025 - Python also provides the math.fmod() function from the math module as an alternative for floating-point modulo operations, which may handle certain edge cases differently:
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.fmod.html
numpy.fmod โ€” NumPy v2.4 Manual
This is the NumPy implementation of the C library function fmod, the remainder has the same sign as the dividend x1. It is equivalent to the Matlab(TM) rem function and should not be confused with the Python modulus operator x1 % x2.