VOOZH about

URL: https://www.geeksforgeeks.org/java/longmath-class-guava-java/

⇱ LongMath Class | Guava | Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

LongMath Class | Guava | Java

Last Updated : 11 Jul, 2025
LongMath is used to perform mathematical operations on Long values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports only the relevant subset of functions. Declaration : The declaration for com.google.common.math.LongMath class is :
@GwtCompatible(emulated = true)
public final class LongMath
 extends Object
Below table shows some of the methods provided by LongMath Class of Guava : 👁 Image
Exceptions :
  • log2 : IllegalArgumentException if x <= 0
  • log10 : IllegalArgumentException if x <= 0
  • pow : IllegalArgumentException if k < 0
  • sqrt : IllegalArgumentException if x < 0
  • divide : ArithmeticException if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b
  • mod : ArithmeticException if m <= 0
  • gcd : IllegalArgumentException if a < 0 or b < 0
  • checkedAdd : ArithmeticException if a + b overflows in signed long arithmetic
  • checkedSubtract : ArithmeticException if a - b overflows in signed long arithmetic
  • checkedMultiply : ArithmeticException if a * b overflows in signed long arithmetic
  • checkedPow : ArithmeticException if b to the kth power overflows in signed long arithmetic
  • factorial : IllegalArgumentException if n < 0
  • binomial : IllegalArgumentException if n < 0, k < 0 or k > n
Some other methods provided by LongMath Class of Guava are : 👁 Image
Example 1 : Output :
Error Message is : mode was UNNECESSARY, but rounding was necessary
Example 2 :
Output :
30
GCD is : 14
Log10 is : 3
modulus is : 0
factorial is : 5040
Log2 is : 3
sqrt is : 12
Reference : Google Guava
Comment
Article Tags:
Article Tags: