math.fmod

Full name
math.fmod
Library
math
Syntax

math.fmod(a, b)

Description

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 of b .

Note that this criterion differs from the one used in Python's % operator, which -in addition to applying a different method for the calculation- returns a result that has the sign of b.

Result

The result returned by the math.fmod function is a real number.

Examples

The modulus of 11 and 4 is 3.0:

math.fmod(11, 4)

3.0

Note that the result is a real number even though the arguments passed to the function are integers.

In this case, the result obtained matches the one returned by the Python % operator:

11 % 4

3

The modulus of -11 and 4 is -3.0:

math.fmod(-11, 4)

-3.0

In this second example, the result obtained does not match the one returned by the % operator in Python:

-11 % 4

1

Other examples are shown below:

math.fmod(-11, -4)

-3.0

math.fmod(1, -4)

1.0

Submitted by admin on Wed, 01/20/2021 - 11:49