Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python math.remainder() Method



The Python math.remainder() method is used to calculate the remainder of dividing one number by another. Mathematically it is denoted as −

remainder(x, y) = x − y × ⌊x/y⌋

Where, x is the dividend, y is the divisor, and ⌊.⌋ denotes the floor method, returning the largest integer less than or equal to the argument. For example, if you have "x = 10.5" and "y = 3.0", then "math.remainder(10.5, 3.0)" will calculate the remainder as 10.5 − 3.0 × ⌊10.5/3.0⌋ = 1.5.

Syntax

Following is the basic syntax of the Python math.prod() method −

math.remainder(x, y)

Parameters

This method accepts the following parameters −

  • x − It is the dividend (the number being divided).

  • y − It is the divisor (the number by which x is being divided).

Return Value

The method returns the remainder of the division of "x" by "y".

Example 1

In the following example, we are calculating the remainder of dividing "10" by "3" using the math.remainder() method −

import math
result = math.remainder(10, 3)
print("The result obtained is:",result)         

Output

The output obtained is as follows −

The result obtained is: 1.0

Example 2

When we pass a negative dividend as an argument to the remainder() method, it retains the sign of the dividend and returns it accordingly −

import math
result = math.remainder(-10, 3)
print("The result obtained is:",result)  

Output

Following is the output of the above code −

The result obtained is: -1.0

Example 3

Now, we are calculating the remainder of dividing the dividend "10" by a negative divisor "-3" using the math.remainder() method −

import math
result = math.remainder(10, -3)
print("The result obtained is:",result) 

Output

We get the output as shown below −

The result obtained is: 1.0

Example 4

In this example, we are calculating the remainder by passing floating-point numbers as argument −

import math
result = math.remainder(7.5, 3.5)
print("The result obtained is:",result) 

Output

The result produced is as shown below −

The result obtained is: 0.5
python_maths.htm
Advertisements