Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

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

Saturday, 18 June 2022

java: math: floorDiv: Get largest integer less than or equal to the algebraic quotient

 

java math provides ‘floorDiv’ method, which returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

 

public static int floorDiv(int x, int y)

Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

 

public static long floorDiv(long x, long y)

Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.

 

Argument x specifies the dividend and y specifies the divisor.

 

Example

Math.floorDiv(5, 3); // Return 1

Math.floorDiv(-5, 3); // Return -2

 

Special cases to consider

a. If the dividend is the Integer.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to the Integer.MIN_VALUE

 

If the dividend is the Long.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to the Long.MIN_VALUE.

 

b. If the signs of the arguments are the same, the results of floorDiv and the / operator are the same.

 

c. If the signs of the arguments are different, the quotient is negative and floorDiv returns the integer less than or equal to the quotient and the / operator returns the integer closest to zero.

For example, floorDiv(-5, 3) == -2, whereas (-5 / 3) == -1.

 


Find the below working application.

 

FloorDivDemo.java

package com.sample.app;

public class FloorDivDemo {

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

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

		// If the dividend is the Integer.MIN_VALUE and the divisor is -1, then integer
		// overflow occurs and the result is equal to the Integer.MIN_VALUE
		// If the dividend is the Long.MIN_VALUE and the divisor is -1, then integer
		// overflow occurs and the result is equal to the Long.MIN_VALUE.
		System.out.printf("Math.floorDiv(%d, %d) is %d\n", Integer.MIN_VALUE, -1, Math.floorDiv(Integer.MIN_VALUE, -1));
		System.out.printf("Math.floorDiv(%d, %d) is %d\n", Long.MIN_VALUE, -1l, Math.floorDiv(Long.MIN_VALUE, -1l));

		// If the signs of the arguments are the same, the results of floorDiv and the /
		// operator are the same.
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", 5, 3, Math.floorDiv(5, 3), 5, 3, (5 / 3));
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", -5, -3, Math.floorDiv(-5, -3), -5, -3, (-5 / -3));
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", 5l, 3l, Math.floorDiv(5l, 3l), 5l, 3l,(5l / 3l));
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", -5, -3l, Math.floorDiv(5l, 3l), -5l, -3l,(-5l / -3l));

		// If the signs of the arguments are different, the quotient is negative and
		// floorDiv returns the integer less than or equal to the quotient and the /
		// operator returns the integer closest to zero.
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", -5, 3, Math.floorDiv(-5, 3), -5, 3,(-5 / 3));
	}

}

 

Output

Math.floorDiv(5, 3) is 1
Math.floorDiv(-5, 3) is -2
Math.floorDiv(5, 3) is 1
Math.floorDiv(-5, 3) is -2
Math.floorDiv(-2147483648, -1) is -2147483648
Math.floorDiv(-9223372036854775808, -1) is -9223372036854775808
Math.floorDiv(5, 3) is 1 and (5/3) = 1
Math.floorDiv(-5, -3) is 1 and (-5/-3) = 1
Math.floorDiv(5, 3) is 1 and (5/3) = 1
Math.floorDiv(-5, -3) is 1 and (-5/-3) = 1
Math.floorDiv(-5, 3) is -2 and (-5/3) = -1

 

 

 

Previous                                                 Next                                                 Home

Thursday, 16 June 2022

Java math: floor: get largest double value that is less than or equal to this number

Java math ‘floor’ method Returns the largest double value that is less than or equal to the argument and is equal to a mathematical integer.

 

Signature

public static double floor(double a)

 

Special cases to consider

a. If the argument value is already equal to a mathematical integer, then the result is the same as the argument.

 

b. If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

 

Find the below working application.

 

FloorMethodDemo.java

package miscellaneous;

public class FloorMethodDemo {

	public static void main(String[] args) {
		double d1 = 10.9;
		double d2 = -10.9;

		System.out.printf("floor(%f) is %f\n", d1, Math.floor(d1));
		System.out.printf("floor(%f) is %f\n", d2, Math.floor(d2));

		// If the argument value is already equal to a mathematical integer, then the
		// result is the same as the argument.
		double d3 = 10;
		double d4 = -10;
		System.out.printf("floor(%f) is %f\n", d3, Math.floor(d3));
		System.out.printf("floor(%f) is %f\n", d4, Math.floor(d4));

		// If the argument is NaN or an infinity or positive zero or negative zero, then
		// the result is the same as the argument.
		System.out.printf("floor(%f) is %f\n", Double.NaN, Math.floor(Double.NaN));
		System.out.printf("floor(%f) is %f\n", Double.POSITIVE_INFINITY, Math.floor(Double.POSITIVE_INFINITY));
		System.out.printf("floor(%f) is %f\n", Double.NEGATIVE_INFINITY, Math.floor(Double.NEGATIVE_INFINITY));
		System.out.printf("floor(%f) is %f\n", 0.0, Math.floor(0.0));
		System.out.printf("floor(%f) is %f\n", -0.0, Math.floor(-0.0));
	}

}

 

Output

floor(10.900000) is 10.000000
floor(-10.900000) is -11.000000
floor(10.000000) is 10.000000
floor(-10.000000) is -10.000000
floor(NaN) is NaN
floor(Infinity) is Infinity
floor(-Infinity) is -Infinity
floor(0.000000) is 0.000000
floor(-0.000000) is -0.000000

 

 

 

  

Previous                                                 Next                                                 Home

Sunday, 12 June 2022

java math: expm1: return e power x minus 1

Java math 'expm1' method returns  ex -1. Note that for values of x near 0, the exact sum of expm1(x) + 1 is much closer to the true result of ex than exp(x).

 

Signature

public static double expm1(double x)

 

Special cases to consider

a.   If the argument is NaN, the result is NaN.

b.   If the argument is positive infinity, then the result is positive infinity.

c.    If the argument is negative infinity, then the result is -1.0.

d.   If the argument is zero, then the result is a zero with the same sign as the argument.

 

Find the below working application.

 

ExpmOneDemo.java

package com.sample.app;

public class ExpmOneDemo {

	public static void main(String[] args) {
		double d1 = 10.0;

		System.out.printf("expm1(%f) : %f\n", d1, Math.expm1(d1));

		// If the argument is NaN, the result is NaN.
		System.out.printf("expm1(%f) : %f\n", Double.NaN, Math.expm1(Double.NaN));

		// If the argument is positive infinity, then the result is positive infinity.
		System.out.printf("expm1(%f) : %f\n", Double.POSITIVE_INFINITY, Math.expm1(Double.POSITIVE_INFINITY));

		// If the argument is negative infinity, then the result is -1.0
		System.out.printf("expm1(%f) : %f\n", Double.NEGATIVE_INFINITY, Math.expm1(Double.NEGATIVE_INFINITY));

		// If the argument is zero, then the result is a zero with the same sign as the
		// argument.
		System.out.printf("expm1(%f) : %f\n", 0.0, Math.expm1(0.0));
		System.out.printf("expm1(%f) : %f\n", -0.0, Math.expm1(-0.0));

	}

}

 


Output

 

expm1(10.000000) : 22025.465795
expm1(NaN) : NaN
expm1(Infinity) : Infinity
expm1(-Infinity) : -1.000000
expm1(0.000000) : 0.000000
expm1(-0.000000) : -0.000000




Previous                                                 Next                                                 Home

Thursday, 9 June 2022

Java math: exp(): get e to the power of a number

Java math exp() method returns Euler's number e raised to the power of a double value.

 

Signature

public static double exp(double a)

 

Special cases to consider

a. If the argument is NaN, the result is NaN.

b. If the argument is positive infinity, then the result is positive infinity.

c. If the argument is negative infinity, then the result is positive zero.

 

Find the below working application.

 

ExpMethodDemo.java

package com.sample.app;

public class ExpMethodDemo {
	
	public static void main(String[] args) {
		System.out.printf("exp(%f) is %f\n", 10.0, Math.exp(10.0));
		
		// If the argument is NaN, the result is NaN.
		System.out.printf("exp(%f) is %f\n", Double.NaN, Math.exp(Double.NaN));
		
		// If the argument is positive infinity, then the result is positive infinity.
		System.out.printf("exp(%f) is %f\n", Double.POSITIVE_INFINITY, Math.exp(Double.POSITIVE_INFINITY));
		
		// If the argument is negative infinity, then the result is positive zero.
		System.out.printf("exp(%f) is %f\n", Double.NEGATIVE_INFINITY, Math.exp(Double.NEGATIVE_INFINITY));
		
	}

}

 

Output

exp(10.000000) is 22026.465795
exp(NaN) is NaN
exp(Infinity) is Infinity
exp(-Infinity) is 0.000000

 

  

Previous                                                 Next                                                 Home

Tuesday, 7 June 2022

Java math decrementExact: Decrement the number by 1 and throw exception if the result overflows

Java Math provides ‘decrementExact’ method, which is used to decremement the number by 1. It is available in below overloaded forms.

 

public static int decrementExact(int a)

Returns the argument decremented by one, throwing an ArithmeticException if the result overflows an int.

 

public static long decrementExact(long a)

Returns the argument decremented by one, throwing an ArithmeticException if the result overflows a long.

 

Find the below working application.

 

DecrementExactDemo.java

package com.sample.app;

public class DecrementExactDemo {

	private static void decrementExact(int number) {
		try {
			System.out.printf("decrementExact(%d) is %d\n", number, Math.decrementExact(number));
		} catch (ArithmeticException e) {
			System.out.println("Error occurred while decremeting the number : " + number);
			e.printStackTrace();
		}
	}

	private static void decrementExact(long number) {
		try {
			System.out.printf("decrementExact(%d) is %d\n", number, Math.decrementExact(number));
		} catch (ArithmeticException e) {
			e.printStackTrace();
			System.out.println();
		}
	}

	public static void main(String[] args) {
		int a = 10;
		int b = Integer.MIN_VALUE;

		decrementExact(a);
		decrementExact(b);

		System.out.println();
		
		long c = 10l;
		long d = Long.MIN_VALUE;

		decrementExact(c);
		decrementExact(d);

	}

}

 


Output

decrementExact(10) is 9
Error occurred while decremeting the number : -2147483648
java.lang.ArithmeticException: integer overflow
	at java.lang.Math.decrementExact(Math.java:943)
	at com.sample.app.DecrementExactDemo.decrementExact(DecrementExactDemo.java:7)
	at com.sample.app.DecrementExactDemo.main(DecrementExactDemo.java:28)

decrementExact(10) is 9
java.lang.ArithmeticException: long overflow
	at java.lang.Math.decrementExact(Math.java:960)
	at com.sample.app.DecrementExactDemo.decrementExact(DecrementExactDemo.java:16)
	at com.sample.app.DecrementExactDemo.main(DecrementExactDemo.java:36)

 

 

Previous                                                 Next                                                 Home

Monday, 6 June 2022

Java math: copySign: Copy the sign of argument

‘copySign’ method returns the first floating-point argument with the sign of the second floating-point argument.

 

Signature

public static double copySign(double a, double b)

public static float copySign(float a, float b)

 

Return the value of ‘a’ by copying the sign of value ‘b’.

 

Examples

Math.copySign(10.030000, 114.000000) = 10.030000

Math.copySign(10.030000, -10.800000) : -10.030000

Math.copySign(-10.800000, 114.000000) : 10.800000

Math.copySign(-10.800000, -11.500000) : -10.800000

 

Find the below working application.

 


 

CopySign.java

package com.sample.app;

public class CopySign {
	public static void main(String[] args) {
		
		float f1 = 10.03f;
		float f2 = 114f;
		float f3 = -10.8f;
		float f4 = -11.5f;
		
		System.out.printf("Math.copySign(%f, %f) = %f\n", f1, f2, Math.copySign(f1, f2));
		System.out.printf("Math.copySign(%f, %f) = %f\n", f1, f3, Math.copySign(f1, f3));
		System.out.printf("Math.copySign(%f, %f) = %f\n", f3, f2, Math.copySign(f3, f2));
		System.out.printf("Math.copySign(%f, %f) = %f\n", f3, f4, Math.copySign(f3, f4));
		
		double d1 = 10.03;
		double d2 = 114;
		double d3 = -10.8;
		double d4 = -11.5;
		
		System.out.printf("\nMath.copySign(%f, %f) = %f\n", d1, d2, Math.copySign(d1, d2));
		System.out.printf("Math.copySign(%f, %f) = %f\n", d1, d3, Math.copySign(d1, d3));
		System.out.printf("Math.copySign(%f, %f) = %f\n", d3, d2, Math.copySign(d3, d2));
		System.out.printf("Math.copySign(%f, %f) = %f\n", d3, d4, Math.copySign(d3, d4));
		
	}

}

Output

Math.copySign(10.030000, 114.000000) = 10.030000
Math.copySign(10.030000, -10.800000) : -10.030000
Math.copySign(-10.800000, 114.000000) : 10.800000
Math.copySign(-10.800000, -11.500000) : -10.800000

Math.copySign(10.030000, 114.000000) = 10.030000
Math.copySign(10.030000, -10.800000) : -10.030000
Math.copySign(-10.800000, 114.000000) : 10.800000
Math.copySign(-10.800000, -11.500000) : -10.800000



Previous                                                 Next                                                 Home