Guava - MATH UTILITIESGuava provides Mathematics related Utilities classes to handle int, long and BigInteger. Following is the list of useful utilities:
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
This class inherits methods from the following 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
This class inherits methods from the following 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
This class inherits methods from the following 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 |