001/*
002 * Copyright (C) 2020 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.math;
016
017import com.google.common.annotations.GwtIncompatible;
018import java.math.BigDecimal;
019import java.math.RoundingMode;
020
021/**
022 * A class for arithmetic on {@link BigDecimal} that is not covered by its built-in methods.
023 *
024 * @author Louis Wasserman
025 * @since 30.0
026 */
027@GwtIncompatible
028public class BigDecimalMath {
029  private BigDecimalMath() {}
030
031  /**
032   * Returns {@code x}, rounded to a {@code double} with the specified rounding mode. If {@code x}
033   * is precisely representable as a {@code double}, its {@code double} value will be returned;
034   * otherwise, the rounding will choose between the two nearest representable values with {@code
035   * mode}.
036   *
037   * <p>For the case of {@link RoundingMode#HALF_DOWN}, {@code HALF_UP}, and {@code HALF_EVEN},
038   * infinite {@code double} values are considered infinitely far away. For example, 2^2000 is not
039   * representable as a double, but {@code roundToDouble(BigDecimal.valueOf(2).pow(2000), HALF_UP)}
040   * will return {@code Double.MAX_VALUE}, not {@code Double.POSITIVE_INFINITY}.
041   *
042   * <p>For the case of {@link RoundingMode#HALF_EVEN}, this implementation uses the IEEE 754
043   * default rounding mode: if the two nearest representable values are equally near, the one with
044   * the least significant bit zero is chosen. (In such cases, both of the nearest representable
045   * values are even integers; this method returns the one that is a multiple of a greater power of
046   * two.)
047   *
048   * @throws ArithmeticException if {@code mode} is {@link RoundingMode#UNNECESSARY} and {@code x}
049   *     is not precisely representable as a {@code double}
050   * @since 30.0
051   */
052  public static double roundToDouble(BigDecimal x, RoundingMode mode) {
053    return BigDecimalToDoubleRounder.INSTANCE.roundToDouble(x, mode);
054  }
055
056  private static class BigDecimalToDoubleRounder extends ToDoubleRounder<BigDecimal> {
057    static final BigDecimalToDoubleRounder INSTANCE = new BigDecimalToDoubleRounder();
058
059    private BigDecimalToDoubleRounder() {}
060
061    @Override
062    double roundToDoubleArbitrarily(BigDecimal bigDecimal) {
063      return bigDecimal.doubleValue();
064    }
065
066    @Override
067    int sign(BigDecimal bigDecimal) {
068      return bigDecimal.signum();
069    }
070
071    @Override
072    BigDecimal toX(double d, RoundingMode mode) {
073      return new BigDecimal(d);
074    }
075
076    @Override
077    BigDecimal minus(BigDecimal a, BigDecimal b) {
078      return a.subtract(b);
079    }
080  }
081}