< prev index next >

src/java.base/share/classes/java/math/BigDecimal.java

Print this page




1976      * @throws ArithmeticException if the result is inexact but the
1977      *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
1978      *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
1979      *         require a precision of more than {@code mc.precision} digits.
1980      * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
1981      * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
1982      * @since  1.5
1983      */
1984     public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) {
1985         if (mc.precision == 0)
1986             return divideAndRemainder(divisor);
1987 
1988         BigDecimal[] result = new BigDecimal[2];
1989         BigDecimal lhs = this;
1990 
1991         result[0] = lhs.divideToIntegralValue(divisor, mc);
1992         result[1] = lhs.subtract(result[0].multiply(divisor));
1993         return result;
1994     }
1995 


































1996     /**
1997      * Returns a {@code BigDecimal} whose value is
1998      * <code>(this<sup>n</sup>)</code>, The power is computed exactly, to
1999      * unlimited precision.
2000      *
2001      * <p>The parameter {@code n} must be in the range 0 through
2002      * 999999999, inclusive.  {@code ZERO.pow(0)} returns {@link
2003      * #ONE}.
2004      *
2005      * Note that future releases may expand the allowable exponent
2006      * range of this method.
2007      *
2008      * @param  n power to raise this {@code BigDecimal} to.
2009      * @return <code>this<sup>n</sup></code>
2010      * @throws ArithmeticException if {@code n} is out of range.
2011      * @since  1.5
2012      */
2013     public BigDecimal pow(int n) {
2014         if (n < 0 || n > 999999999)
2015             throw new ArithmeticException("Invalid operation");




1976      * @throws ArithmeticException if the result is inexact but the
1977      *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
1978      *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
1979      *         require a precision of more than {@code mc.precision} digits.
1980      * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
1981      * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
1982      * @since  1.5
1983      */
1984     public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) {
1985         if (mc.precision == 0)
1986             return divideAndRemainder(divisor);
1987 
1988         BigDecimal[] result = new BigDecimal[2];
1989         BigDecimal lhs = this;
1990 
1991         result[0] = lhs.divideToIntegralValue(divisor, mc);
1992         result[1] = lhs.subtract(result[0].multiply(divisor));
1993         return result;
1994     }
1995 
1996 
1997     /**
1998      * Returns an approximation to the square root of {@code this}
1999      * with rounding according to the context settings.
2000      *
2001      * <p>The preferred scale of the returned result is equal to
2002      * {@code floor(this.scale()/2.0)}. The value of the returned
2003      * result is always within one ulp of the exact decimal value for
2004      * the precision in question.  If the rounding mode is {@link
2005      * RoundingMode#HALF_UP HALF_UP}, {@link RoundingMode#HALF_DOWN
2006      * HALF_DOWN}, or {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
2007      * result is within one half an ulp of the exact decimal value.
2008      *
2009      * <p>Special case:
2010      * <ul>
2011      * <li> The square root of {@code ZERO} is {@code ZERO}
2012      * </ul>
2013      *
2014      * @param mc the context to use.
2015      * @return the square root of {@code this}.
2016      * @throws ArithmeticException if {@code this} is less than zero.
2017      * @throws ArithmeticException if an exact result is requested
2018      * ({@code mc.getPrecision()==0}) and there is no finite decimal
2019      * expansion of the exact result
2020      * @throws ArithmeticException if
2021      * {@code (mc.getRoundingMode()==RoundingMode.UNNECESSARY}) and
2022      * the exact result cannot fit in {@code mc.getPrecision()}
2023      * digits.
2024      * @since  9
2025      */
2026     BigDecimal sqrt(MathContext mc) {
2027         return ZERO; // Just the specification for now.
2028     }
2029 
2030     /**
2031      * Returns a {@code BigDecimal} whose value is
2032      * <code>(this<sup>n</sup>)</code>, The power is computed exactly, to
2033      * unlimited precision.
2034      *
2035      * <p>The parameter {@code n} must be in the range 0 through
2036      * 999999999, inclusive.  {@code ZERO.pow(0)} returns {@link
2037      * #ONE}.
2038      *
2039      * Note that future releases may expand the allowable exponent
2040      * range of this method.
2041      *
2042      * @param  n power to raise this {@code BigDecimal} to.
2043      * @return <code>this<sup>n</sup></code>
2044      * @throws ArithmeticException if {@code n} is out of range.
2045      * @since  1.5
2046      */
2047     public BigDecimal pow(int n) {
2048         if (n < 0 || n > 999999999)
2049             throw new ArithmeticException("Invalid operation");


< prev index next >