Monday 20 June 2022

Java math: floorMod: Get floor modulus of int/long arguments

 

Java Math class provides floorMod method which return the floor modulus of the int/long arguments.

 

Signature

public static int floorMod(int x, int y)

public static long floorMod(long x, long y)

'x' is the dividend and 'y' is the divisor.

 

Special cases to consider

a. If the signs of the arguments are the same, the results of floorMod and the % operator are the same.

Math.floorMod(5, 3) is 2 and (5 % 3) is 2

Math.floorMod(-5, -3) is -2 and (-5 % -3) is -2

Math.floorMod(5l, 3l) is 2 and (5l % 3l) is 2

Math.floorMod(-5l, -3l) is -2 and (-5l % -3l) is -2

 

b. If the signs of the arguments are different, the results differ from the % operator.

 

Math.floorMod(5, -3) is -1 and (5 % -3) is 2

Math.floorMod(-5, 3) is 1 and (-5 % 3) is -2

Math.floorMod(5l, -3l) is -1 and (5l % -3l) is 2

Math.floorMod(-5l, 3l) is 1 and (-5l % 3l) is -2

 

Relationship between floorDiv and floorMod

floorDiv(x, y) * y + floorMod(x, y) == x

 

floorDiv(10, 3) * 3 + floorMod(10, 3) = 10

floorDiv(10, -3) * -3 + floorMod(10, -3) = 10

floorDiv(-10, 3) * 3 + floorMod(-10, 3) = -10

floorDiv(-10, -3) * -3 + floorMod(-10, -3) = -10

 

Find the below working application.

 


FloorModDemo.java

package com.sample.app;

public class FloorModDemo {
	public static void main(String[] args) {
		System.out.printf("Math.floorMod(%d, %d) is %d\n", 5, 3, Math.floorMod(5, 3));
		System.out.printf("Math.floorMod(%d, %d) is %d\n", -5, 3, Math.floorMod(-5, 3));

		System.out.printf("Math.floorMod(%d, %d) is %d\n", 5l, 3l, Math.floorMod(5l, 3l));
		System.out.printf("Math.floorMod(%d, %d) is %d\n", -5l, 3l, Math.floorMod(-5l, 3l));

		// If the signs of the arguments are the same, the results of floorMod and the %
		// operator are the same.
		System.out.println("\nMath.floorMod(5, 3) is " + Math.floorMod(5, 3) + " and (5 % 3) is " + (5 % 3));
		System.out.println("Math.floorMod(-5, -3) is " + Math.floorMod(-5, -3) + " and (-5 % -3) is " + (-5 % -3));

		System.out.println("Math.floorMod(5l, 3l) is " + Math.floorMod(5l, 3l) + " and (5l % 3l) is " + (5l % 3l));
		System.out.println("Math.floorMod(-5l, -3l) is " + Math.floorMod(-5l, -3l) + " and (-5l % -3l) is " + (-5l % -3l));

		// If the signs of the arguments are different, the results differ from the % operator.
		System.out.println("\nMath.floorMod(5, -3) is " + Math.floorMod(5, -3) + " and (5 % -3) is " + (5 % -3));
		System.out.println("Math.floorMod(-5, 3) is " + Math.floorMod(-5, 3) + " and (-5 % 3) is " + (-5 % 3));
		System.out.println("Math.floorMod(5l, -3l) is " + Math.floorMod(5l, -3l) + " and (5l % -3l) is " + (5l % -3l));
		System.out.println("Math.floorMod(-5l, 3l) is " + Math.floorMod(-5l, 3l) + " and (-5l % 3l) is " + (-5l % 3l));
		
		// floorDiv(x, y) * y + floorMod(x, y) == x
		int x = 10, y=3;
		System.out.printf("\nfloorDiv(%d, %d) * %d + floorMod(%d, %d) = %d", x, y, y, x, y, x);
		
		x = 10; y=-3;
		System.out.printf("\nfloorDiv(%d, %d) * %d + floorMod(%d, %d) = %d", x, y, y, x, y, x);
		
		x = -10; y=3;
		System.out.printf("\nfloorDiv(%d, %d) * %d + floorMod(%d, %d) = %d", x, y, y, x, y, x);
		
		x = -10; y=-3;
		System.out.printf("\nfloorDiv(%d, %d) * %d + floorMod(%d, %d) = %d", x, y, y, x, y, x);
	}

}

 

Output

Math.floorMod(5, 3) is 2
Math.floorMod(-5, 3) is 1
Math.floorMod(5, 3) is 2
Math.floorMod(-5, 3) is 1

Math.floorMod(5, 3) is 2 and (5 % 3) is 2
Math.floorMod(-5, -3) is -2 and (-5 % -3) is -2
Math.floorMod(5l, 3l) is 2 and (5l % 3l) is 2
Math.floorMod(-5l, -3l) is -2 and (-5l % -3l) is -2

Math.floorMod(5, -3) is -1 and (5 % -3) is 2
Math.floorMod(-5, 3) is 1 and (-5 % 3) is -2
Math.floorMod(5l, -3l) is -1 and (5l % -3l) is 2
Math.floorMod(-5l, 3l) is 1 and (-5l % 3l) is -2

floorDiv(10, 3) * 3 + floorMod(10, 3) = 10
floorDiv(10, -3) * -3 + floorMod(10, -3) = 10
floorDiv(-10, 3) * 3 + floorMod(-10, 3) = -10
floorDiv(-10, -3) * -3 + floorMod(-10, -3) = -10

 

 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment