Trusted answers to developer questions

What is math.fmod() in python?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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(). A modulo is the remainder left when a number is divided by another number.

Syntax

math.fmod(x, y)

Arguments

  1. x: A number (positive or negative) to divide. This parameter is required.
  2. y: A number (positive or negative) to divide by x. This parameter is required.

Return Value

This function returns a float value that represents the remainder of x and y. A ValueError is returned if both x and y are zero. The same error is returned when y is zero, but not when x is zero. A TypeError is returned if x or y are not numbers.

Code Example

#import library
import math
#display remainders of x and y
print('10 % 4:',math.fmod(10, 4))
print('200 % 5:',math.fmod(200, 5))
print('-17 % 3:',math.fmod(-17, 3))
print('15 % -4:',math.fmod(15, -4))
print('0 % 7:',math.fmod(0, 7))
print('0 % -5:',math.fmod(0, -5))
print('34 % -7:',math.fmod(34, -7))
print('18 % 4:',math.fmod(18, 4))

RELATED TAGS

python
Did you find this helpful?