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
🌐
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.
🌐
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.
Find elsewhere
🌐
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....
🌐
Runoob
runoob.com › python3 › ref-math-fmod.html
Python math.fmod() 方法 | 菜鸟教程
Python math.fmod(x, y) 方法返回 x/y 的余数。 · Python 版本:2.7 · math.fmod() 方法语法如下: · math.fmod(x, y) 参数说明: · x -- 必需,正数或负数。被除数。如果 x 不是一个数字,返回 TypeError。 · y -- 必需,正数或负数。除数。如果 ...
🌐
W3Schools
w3schools.com › c › ref_math_fmod.php
C Math fmod() Function
The fmod() function returns the floating point remainder of the division dividend / divisor where the result of the division is truncated (the decimal part is removed).
🌐
Interactive Chaos
interactivechaos.com › en › python › function › mathremainder
math.remainder | Interactive Chaos
The math.remainder function returns the remainder of the division of "x" and "y" according to the criteria imposed by the IEEE 754 standard. This remainder is the difference of x-(n*y) for the integer value n closest to the integer part of the x/y quotient (be it greater or less than this quotient).
🌐
O Level Notes
olevelnotes.com › home › lessons › math.fmod()
Math.fmod() - O Level Notes
June 3, 2024 - Math.fmod():- The math.fmod() method returns the remainder (modulo) of x/y. Example Return the remainder of x/y: import math print(math.fmod(20, 4)) print(math.fmod(20, 3)) print(math.fmod(15, 6)) print(math.fmod(-10, 3)) print(math.fmod(0, 0)) Output: 0.0 2.0 3.0 -1.0 print(math.fmod(0, 0)) ...
🌐
Python
docs.python.org › fr › 3 › library › math.html
math --- Mathematical functions — Documentation Python 3.14.3
February 21, 2026 - For example, fmod(-1e-100, 1e100) is -1e-100, but the result of Python's -1e-100 % 1e100 is 1e100-1e-100, which cannot be represented exactly as a float, and rounds to the surprising 1e100. For this reason, function fmod() is generally preferred when working with floats, while Python's x % y is preferred when working with integers.
🌐
Wilfried-grupe
wilfried-grupe.de › py_Mathedemo.html
Python: math
April 17, 2021 - Loading your experience… This won’t take long
🌐
YouTube
youtube.com › watch
fmod() function of math module in python - YouTube
Fmod function of math module...Guys watch and comment below in the comments section..For more enquiries mail at :- GoAloneGrowYourself@Gmail.comSee Here what...
Published   June 22, 2020
🌐
Uwaterloo
open.cs.uwaterloo.ca › python-from-scratch › 2 › 3 › transcript
Built-in functions in Python (Part 1)
If you are using a module, this should be the first line of your program. To use a function that is in the module, use dot notation to join the name of the module with the name of the function. math.sqrt is the square root function and math.fmod is the modulo function.
Top answer
1 of 3
3

As Thomas K said, use math.fmod for modulo, or if you really want you can define it yourself:

def cmod(x, y):
    return abs(x) % abs(y) * (1 if x > 0 else -1)

And this function should emulate C-style division:

def cdiv(x, y):
    return abs(x) / abs(y) * cmp(x, 0) * cmp(y, 0)

You said that you must use the / and % operators. This is not possible, since you can't override the operator for built-ins. You can however define your own integer type and operator overload the __div__ and __mod__ operators.

2 of 3
2

There is no flag you can set to make python division to act like c++.

You advised that you can't code your own division function, but if you change your mind you can do this:

def cpp_int_div(dividend, divisor):
    a, b = dividend, divisor
    sign = 1 if (a>0 and b>0) or (a<0 and b<0) else -1
    return (abs(a)/abs(b)) * sign

def cpp_int_mod(dividend, divisor): # or just use math.fmod  (from Thomas K)
    a, b = dividend, divisor
    sign = 1 if a>0 else -1
    return (abs(a)%abs(b)) * sign

This shows that it acts according to your specification:

print "11 / 3 = %d" % cpp_int_div(11,3)
print "11 %% 3 = %d" % cpp_int_mod(11,3)
print "(-11) / 3 = %d" % cpp_int_div(-11, 3)
print "(-11) %% 3 = %d" % cpp_int_mod(-11, 3)
print "11 / (-3) = %d" % cpp_int_div(11, -3)
print "11 %% (-3) = %d" % cpp_int_mod(11, -3)
print "(-11) / (-3) = %d" % cpp_int_div(-11, -3)
print "(-11) %% (-3) = %d" % cpp_int_mod(-11, -3)

Which gives:

11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -3
(-11) % 3 = -2
11 / (-3) = -3
11 % (-3) = 2
(-11) / (-3) = 3
(-11) % (-3) = -2