Java >> Java tutorial >  >> Tag >> class

BigIntegerMath Class | Guava | Java

BigIntegerMath bruges til at udføre matematiske operationer på BigInteger-værdier. Grundlæggende selvstændige matematiske funktioner er opdelt i klasserne IntMath, LongMath, DoubleMath og BigIntegerMath baseret på den involverede primære numeriske type. Disse klasser har parallel struktur, men hver enkelt understøtter kun den relevante delmængde af funktioner. Lignende funktionalitet for int og for long kan findes i henholdsvis IntMath og LongMath.

Erklæring : Erklæringen for com.google.common.math.BigIntegerMath klasse er :

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

Nedenstående tabel viser metoderne leveret af BigIntegerMath Class of Guava :

Undtagelser:

  • log2 : UllegalArgumentException hvis x <=0
  • log10 : UllegalArgumentException hvis x <=0
  • sqrt : UllegalArgumentException hvis x <0
  • dele : ArithmeticException hvis q ==0, eller hvis tilstand ==UNØDVENDIG og a ikke er et heltal af b
  • faktoriel : UllegalArgumentException hvis n <0
  • binomial : UllegalArgumentException hvis n <0, k n

Eksempel 1 :




// Java code to show implementation of // BigIntegerMath Class of Guava import java.math.*; import com.google.common.math.BigIntegerMath;   class GFG {        // Driver code      public static void main(String args[])      {            // Creating an object of GFG class          GFG obj = new GFG();            // Function calling          obj.examples();      }        private void examples()      {            try {                // exception will be thrown as 10 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 ex) {              System.out.println( "Error Message is : " +                                         ex.getMessage());          }      } }

 

 

Output:

Error Message is : Rounding necessary

Eksempel 2:




// Java code to show implementation of // BigIntegerMath Class of Guava import java.math.*; import com.google.common.math.BigIntegerMath;   class GFG {        // Driver code      public static void main(String args[])      {            // Creating an object of GFG class          GFG obj = new GFG();            // Function calling          obj.examples();      }        private void examples()      {            // As 10 is divisible by 5, so          // no exception is thrown          System.out.println(BigIntegerMath.divide(BigInteger.TEN,                                          new BigInteger( "5" ),                                      RoundingMode.UNNECESSARY));            // To compute log to base 10          System.out.println( "Log10 is : "               BigIntegerMath.log10( new BigInteger( "1000" ),                                    RoundingMode.HALF_EVEN));            // To compute factorial          System.out.println( "factorial is : "                           BigIntegerMath.factorial( 7 ));            // To compute log to base 2          System.out.println( "Log2 is : "             BigIntegerMath.log2( new BigInteger( "8" ),                            RoundingMode.HALF_EVEN));            // To compute square root          System.out.println( "sqrt is : " +                             BigIntegerMath.sqrt(BigInteger.                      TEN.multiply(BigInteger.TEN),                          RoundingMode.HALF_EVEN));      } }

 

 

Output:

2
Log10 is : 3
factorial is : 5040
Log2 is : 3
sqrt is : 10

Reference: Google Guava


Java tag