The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

Guava - MATH UTILITIES


Guava provides Mathematics related Utilities classes to handle int, long and BigInteger. Following is the list of useful utilities:
S.N.Utility name & Description
1IntMath
Math utility for int.
2LongMath
Math utility for long.
3BigIntegerMath
Math utility for BigInteger.
Int Math Class

IntMath provides utility methods on int.

Class Declaration

Following is the declaration for com.google.common.math.IntMath class:

@GwtCompatible(emulated=true)
public final class IntMath
extends Object

Methods
S.N.Method & Description
1static int binomial(int n, int k)
Returns n choose k, also known as the binomial coefficient of n and k, or Integer.MAX_VALUE if the result does not fit in an int.
2static int checkedAdd(int a, int b)
Returns the sum of a and b, provided it does not overflow.
3static int checkedMultiply(int a, int b)
Returns the product of a and b, provided it does not overflow.
4static int checkedPow(int b, int k)
Returns the b to the kth power, provided it does not overflow.
5static int checkedSubtract(int a, int b)
Returns the difference of a and b, provided it does not overflow.
6static int divide(int p, int q, RoundingMode mode)
Returns the result of dividing p by q, rounding using the specified RoundingMode.
7static int factorial(int n)
Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Integer.MAX_VALUE if the result does not fit in a int.
8static int gcd(int a, int b)
Returns the greatest common divisor of a, b.
9static boolean isPowerOfTwo(int x)
Returns true if x represents a power of two.
10static int log10(int x, RoundingMode mode)
Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
11static int log2(int x, RoundingMode mode)
Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
12static int mean(int x, int y)
Returns the arithmetic mean of x and y, rounded towards negative infinity.
13static int mod(int x, int m)
Returns x mod m, a non-negative value less than m.
14static int pow(int b, int k)
Returns b to the kth power.
15static int sqrt(int x, RoundingMode mode)
Returns the square root of x, rounded with the specified rounding mode.
Methods Inherited

This class inherits methods from the following class:
  • java.lang.Object
Example of Int Math Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

import java.math.RoundingMode;
import com.google.common.math.IntMath;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testIntMath();
}
private void testIntMath(){
try{
System.out.println(IntMath.checkedAdd(Integer.MAX_VALUE,
Integer.MAX_VALUE));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}
System.out.println(IntMath.divide(100, 5,
RoundingMode.UNNECESSARY));
try{
//exception will be thrown as 100 is not completely divisible by
3 thus rounding
// is required, and RoundingMode is set as UNNESSARY
System.out.println(IntMath.divide(100, 3,
RoundingMode.UNNECESSARY));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}
System.out.println("Log2(2): "+IntMath.log2(2,
RoundingMode.HALF_EVEN));
System.out.println("Log10(10): "+IntMath.log10(10,
RoundingMode.HALF_EVEN));
System.out.println("sqrt(100): "+IntMath.sqrt(IntMath.pow(10,2),
RoundingMode.HALF_EVEN));
System.out.println("gcd(100,50): "+IntMath.gcd(100,50));
System.out.println("modulus(100,50): "+IntMath.mod(100,50));
System.out.println("factorial(5): "+IntMath.factorial(5));
}
}

Verify the Result

Compile the class using javac compiler as follows:

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

Error: overflow
20
Error: mode was UNNECESSARY, but rounding was necessary
Log2(2): 1
Log10(10): 1
sqrt(100): 10
gcd(100,50): 50
modulus(100,50): 0
factorial(5): 120

Long Math Class

LongMath provides utility methods on long.

Class Declaration

Following is the declaration for com.google.common.math.LongMath class:

@GwtCompatible(emulated=true)
public final class LongMath
extends Object

Methods
S.N.Method & Description
1static long binomial(int n, int k)
Returns n choose k, also known as the binomial coefficient of n and k, or Long.MAX_VALUE if the result does not fit in a long.
2static long checkedAdd(long a, long b)
Returns the sum of a and b, provided it does not overflow.
3static long checkedMultiply(long a, long b)
Returns the product of a and b, provided it does not overflow.
4static long checkedPow(long b, int k)
Returns the b to the kth power, provided it does not overflow.
5static long checkedSubtract(long a, long b)
Returns the difference of a and b, provided it does not overflow.
6static long divide(long p, long q, RoundingMode mode)
Returns the result of dividing p by q, rounding using the specified RoundingMode.
7static long factorial(int n)
Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Long.MAX_VALUE if the result does not fit in a long.
8static long gcd(long a, long b)
Returns the greatest common divisor of a, b.
9static boolean isPowerOfTwo(long x)
Returns true if x represents a power of two.
10static int log10(long x, RoundingMode mode)
Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
11static int log2(long x, RoundingMode mode)
Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
12static long mean(long x, long y)
Returns the arithmetic mean of x and y, rounded toward negative infinity.
13static int mod(long x, int m)
Returns x mod m, a non-negative value less than m.
14static long mod(long x, long m)
Returns x mod m, a non-negative value less than m.
15static long pow(long b, int k)
Returns b to the kth power.
16static long sqrt(long x, RoundingMode mode)
Returns the square root of x, rounded with the specified rounding mode.
Methods Inherited

This class inherits methods from the following class:
  • java.lang.Object
Example of Long Math Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java
import java.math.RoundingMode;
import com.google.common.math.LongMath;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testLongMath();
}
private void testLongMath(){
try{
System.out.println(LongMath.checkedAdd(Long.MAX_VALUE,
Long.MAX_VALUE));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}
System.out.println(LongMath.divide(100, 5,
RoundingMode.UNNECESSARY));
try{
//exception will be thrown as 100 is not completely divisible by
// 3, thus rounding is required, and
// RoundingMode is set as UNNESSARY
System.out.println(LongMath.divide(100, 3, RoundingMode.UNNECESSARY));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}
System.out.println("Log2(2): "+LongMath.log2(2, RoundingMode.HALF_EVEN));
System.out.println("Log10(10): "+LongMath.log10(10,
RoundingMode.HALF_EVEN));
System.out.println("sqrt(100): "+LongMath.sqrt(LongMath.pow(10,2),
RoundingMode.HALF_EVEN));
System.out.println("gcd(100,50): "+LongMath.gcd(100,50));
System.out.println("modulus(100,50): "+LongMath.mod(100,50));
System.out.println("factorial(5): "+LongMath.factorial(5));
}
}

Verify the Result

Compile the class using javac compiler as follows:

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

Error: overflow
20
Error: mode was UNNECESSARY, but rounding was necessary
Log2(2): 1
Log10(10): 1
sqrt(100): 10
gcd(100,50): 50
modulus(100,50): 0
factorial(5): 120

Big Integer Math Class

BigIntegerMath provides utility methods on BigInteger.

Class Declaration

Following is the declaration for com.google.common.math.BigIntegerMath class:

@GwtCompatible(emulated=true)
public final class BigIntegerMath
extends Object

Methods
S.N.Method & Description
1static BigInteger binomial(int n, int k)
Returns n choose k, also known as the binomial coefficient of n and k, that is, n! / (k! (n - k)!).
2static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode)
Returns the result of dividing p by q, rounding using the specified RoundingMode.
3static BigInteger factorial(int n)
Returns n!, that is, the product of the first n positive integers, or 1 if n == 0.
4static boolean isPowerOfTwo(BigInteger x)
Returns true if x represents a power of two.
5static int log10(BigInteger x, RoundingMode mode)
Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
6static int log2(BigInteger x, RoundingMode mode)
Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
7static BigInteger sqrt(BigInteger x, RoundingMode mode)
Returns the square root of x, rounded with the specified rounding mode.
Methods Inherited

This class inherits methods from the following class:
  • java.lang.Object
Example of BigIntegerMath Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

import java.math.BigInteger;
import java.math.RoundingMode;
import com.google.common.math.BigIntegerMath;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testBigIntegerMath();
}
private void testBigIntegerMath(){
System.out.println(BigIntegerMath.divide(BigInteger.TEN, new
BigInteger("2"), RoundingMode.UNNECESSARY));
try{
//exception will be thrown as 100 is not completely divisible by
// 3, thus rounding is required, and
// RoundingMode is set as UNNESSARY
System.out.println(BigIntegerMath.divide(BigInteger.TEN, new
BigInteger("3"), RoundingMode.UNNECESSARY));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}
System.out.println("Log2(2): "+BigIntegerMath.log2(new
BigInteger("2"), RoundingMode.HALF_EVEN));
System.out.println("Log10(10):
"+BigIntegerMath.log10(BigInteger.TEN, RoundingMode.HALF_EVEN));
System.out.println("sqrt(100):
"+BigIntegerMath.sqrt(BigInteger.TEN.multiply(BigInteger.TEN),
RoundingMode.HALF_EVEN));
System.out.println("factorial(5): "+BigIntegerMath.factorial(5));
}
}

Verify the Result

Compile the class using javac compiler as follows:

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

5
Error: Rounding necessary
Log2(2): 1
Log10(10): 1
sqrt(100): 10
factorial(5): 120
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com