Guava 12-mathematical operations

Source: Internet
Author: User
Tags gcd greatest common divisor pow rounds square root

Example
int logfloor = LONGMATH.LOG2 (n, floor), int mustnotoverflow = intmath.checkedmultiply (x, y); long quotient = Longmath.divid E (Knownmultipleofthree, 3, roundingmode.unnecessary); Fail fast on non-multiple of 3BigInteger Nearestinteger = Doublemath.roundtobiginteger (d, Roundingmode.half_even); BigInteger sidelength = bigintegermath.sqrt (area, CEILING);
Why use Guava Math
    • Guava math is fully tested for a variety of unusual overflow situations, as well as a description of overflow semantics and guava documentation, which can cause rapid failure if the overflow check of the operation fails;
    • Guava Math's performance has been carefully designed and tuned, although performance inevitably varies based on specific hardware details, but guava math can often be compared to the mathutils of Apache Commons, and there are even significant improvements in some scenarios.
    • Guava Math has been designed with readability and proper programming in mind, meaning intmath.log2 (x, CEILING) is clearly defined even in fast reading. and 32-integer.numberofleadingzeros (x–1) is not clear enough for the reader.

Note: Guava math and GWT are exceptionally incompatible because the Java and Java script languages have different arithmetic overflow logic.

Integer arithmetic

Guava Math mainly handles three types of integers: int, long, and BigInteger. These three types of arithmetic tools are called Intmath, Longmath, and Bigintegermath, respectively.

Operations with overflow checking

Guava Math provides several computational methods for overflow checking: When results overflow, these methods will fail quickly rather than ignore overflow

Intmath.checkedadd Longmath.checkedadd
Intmath.checkedsubtract Longmath.checkedsubtract
Intmath.checkedmultiply Longmath.checkedmultiply
Intmath.checkedpow Longmath.checkedpow
Intmath.checkedadd (Integer.max_value, Integer.max_value); Throws ArithmeticException
Real arithmetic

IntMath, Longmath, and Bigintegermath provide many methods of real-number arithmetic and round the results of the final operation into integers. These methods accept a Java.math.RoundingMode enumeration value as the rounding pattern:

    • Down: Rounded in 0 direction (de-tailed method)
    • Up: Rounding away from 0 direction
    • Floor: Rounding to negative infinity direction
    • CEILING: Rounding to positive infinity direction
    • Unnecessary: No rounding is required, and if rounded with this mode, it should be thrown directly arithmeticexception
    • HALF_UP: Rounds to the nearest integer, where x.5 is rounded away from the 0 direction
    • Half_down: Rounds to the nearest integer, where x.5 rounds in 0 direction
    • Half_even: Rounds to the nearest integer, where x.5 rounds to adjacent even numbers

These methods are designed to improve the readability of your code, for example, divide (x, 3, CEILING) is clear even when reading quickly. In addition, these methods are implemented internally using the construction of integer approximation recalculation, in addition to floating-point operations when constructing the initial approximation of the sqrt (square root) operation, the whole process of the other methods is integer or bitwise operation, so the performance is better.

Operation IntMath Longmath Bigintegermath
Division Divide (int, int, roundingmode) Divide (long, long, Roundingmode) Divide (BigInteger, BigInteger, Roundingmode)
2 logarithm of the base log2 (int, roundingmode) Log2 (Long, Roundingmode) Log2 (BigInteger, Roundingmode)
10 logarithm of the base log10 (int, roundingmode) LOG10 (Long, Roundingmode) LOG10 (BigInteger, Roundingmode)
Square root sqrt (int, roundingmode) sqrt (long, Roundingmode) sqrt (BigInteger, Roundingmode)
Returns 31622776601683793319988935444327185337195551393252BIGINTEGERMATH.SQRT (BigInteger.TEN.pow (99), Roundingmode.half_even);
Additional Features

Guava also provides a number of useful arithmetic functions

Operation IntMath Longmath Bigintegermath *
Greatest common divisor GCD (int, int) GCD (long, long) BIGINTEGER.GCD (BigInteger)
Take the mold MoD (int, int) MoD (long, long) Biginteger.mod (BigInteger)
Take power POW (int, int) POW (long, int) Biginteger.pow (int)
Whether the power of 2 Ispoweroftwo (int) Ispoweroftwo (Long) Ispoweroftwo (BigInteger)
Factorial factorial (int) factorial (int) factorial (int)
Two-item coefficient * binomial (int, int) binomial (int, int) binomial (int, int)

*biginteger greatest common divisor and modulo operations are provided by the JDK

* Factorial and two-key arithmetic results if overflow, return max_value

Floating-point arithmetic

The JDK completely covers the floating-point arithmetic, but guava also provides some useful methods in the Doublemath class.

Ismathematicalinteger (Double) Determine if the floating-point number is an integer
Roundtoint (double, roundingmode) rounds to int; throws an exception on an infinite decimal, overflow
Roundtolong (double, roundingmode) Rounds to long; throws an exception on an infinite decimal, overflow
Roundtobiginteger (double, roundingmode) Rounds to BigInteger; throws an exception on an infinite decimal
LOG2 (double, roundingmode) 2 floating-point logarithm, and rounded to int, faster than JDK Math.log (double)

Guava 12-mathematical operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.