Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

# What is the Math.floorMod method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

floorMod is a static method of the Math class that returns the floor modulus of the two numbers passed to it. The sign of the return value is the same as the sign of the passed divisor.

The formula to calculate floorMod is as follows:

floorMod(dividend, divisor) = dividend - (floorDiv(dividend, divisor) * divisor)

• If the dividend and divisor are of the same sign, then the results of floorMod and the normal modulo operation are the same.

• If the dividend and divisor are of different signs, then the result of floorMod has the same sign of the divisor.

The relationship between floorMod and floorDiv is as follows:

floorDiv(dividend, divisor) * divisor + floorMod(dividend, divisor) == dividend


The Math.floorMod method is introduced in Java 8.

The floorMod method is defined in the Math class, which is defined in the java.lang package.

To import the Math class, use the statement below:

import java.lang.Math;


### Example 1

• dividend - 23
• divisor - 4

The fractional representation is $23/4$.

• floorDiv(23, 4) = 5
• floorMod(23, 4) = 23 - ( 5 * 4 ) = 23 - 20 = 3

### Example 2

• dividend - -23
• divisor - 4

The fractional representation is $-23/4$.

• floorDiv(-23, 4) = -6
• floorMod(-23, 4) = -23 - ( -6 * 4 ) = -23 - (-24) = -23 + 24 = 1

### Example 3

• dividend - 23
• divisor - -4

The fractional representation is $23/-4$.

• floorDiv(23, -4) = -6
• floorMod(23, -4) = 23 - ( -6 * -4 ) = 23 - 24 = 23 - 24 = -1

### Example 4

• dividend - -23
• divisor - -4

The fractional representation is $-23/-4$.

• floorDiv(-23, -4) = 5
• floorMod(-23, -4) = -23 - ( 5 * -4 ) = -23 - (-20) = -23 + 20 = -3

### Method signature

public static int floorMod(int x, int y)


### Parameters

• int x: dividend
• int y: divisor

### Return value

Math.floorMod() returns the floor modulus.

public static int floorMod(long x, int y)

public static long floorMod(long x, long y)


### Code

import java.lang.Math;

public class Main{

public static void main(String[] args){
int dividend = 23;
int divisor = 4;
int remainder = Math.floorMod(dividend, divisor);
System.out.printf("floorMod(%d, %d) = %d", dividend, divisor, remainder);
System.out.println();

dividend = 23;
divisor = -4;
remainder = Math.floorMod(dividend, divisor);
System.out.printf("floorMod(%d, %d) = %d", dividend, divisor, remainder);
System.out.println();

dividend = -23;
divisor = 4;
remainder = Math.floorMod(dividend, divisor);
System.out.printf("floorMod(%d, %d) = %d", dividend, divisor, remainder);
System.out.println();

dividend = -23;
divisor = -4;
remainder = Math.floorMod(dividend, divisor);
System.out.printf("floorMod(%d, %d) = %d", dividend, divisor, remainder);
System.out.println();
}
}

RELATED TAGS

java
communitycreator

CONTRIBUTOR

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Learn in-demand tech skills in half the time 