Example usage for com.google.common.math BigIntegerMath sqrt

List of usage examples for com.google.common.math BigIntegerMath sqrt

Introduction

In this page you can find the example usage for com.google.common.math BigIntegerMath sqrt.

Prototype

@GwtIncompatible("TODO")
@SuppressWarnings("fallthrough")
public static BigInteger sqrt(BigInteger x, RoundingMode mode) 

Source Link

Document

Returns the square root of x , rounded with the specified rounding mode.

Usage

From source file:org.apache.drill.exec.util.DecimalUtility.java

/**
 * Calculates and returns square root for specified BigDecimal
 * with specified number of digits alter decimal point.
 *
 * @param in    BigDecimal which square root should be calculated
 * @param scale number of digits alter decimal point in the result value.
 * @return square root for specified BigDecimal
 *///from  w ww .  ja va  2 s .  c o  m
public static BigDecimal sqrt(BigDecimal in, int scale) {
    // unscaled BigInteger value from specified BigDecimal with doubled number of digits after decimal point
    // was used to calculate sqrt using Guava's BigIntegerMath.
    BigInteger valueWithDoubleMaxPrecision = in.multiply(BigDecimal.TEN.pow(scale * 2))
            .setScale(0, RoundingMode.HALF_UP).unscaledValue();
    return new BigDecimal(BigIntegerMath.sqrt(valueWithDoubleMaxPrecision, RoundingMode.HALF_UP), scale);
}