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 com.google.common.annotations.J2ktIncompatible; 019import java.math.BigDecimal; 020import java.math.RoundingMode; 021 022/** 023 * A class for arithmetic on {@link BigDecimal} that is not covered by its built-in methods. 024 * 025 * @author Louis Wasserman 026 * @since 30.0 027 */ 028@J2ktIncompatible 029@GwtIncompatible 030public class BigDecimalMath { 031 private BigDecimalMath() {} 032 033 /** 034 * Returns {@code x}, rounded to a {@code double} with the specified rounding mode. If {@code x} 035 * is precisely representable as a {@code double}, its {@code double} value will be returned; 036 * otherwise, the rounding will choose between the two nearest representable values with {@code 037 * mode}. 038 * 039 * <p>For the case of {@link RoundingMode#HALF_DOWN}, {@code HALF_UP}, and {@code HALF_EVEN}, 040 * infinite {@code double} values are considered infinitely far away. For example, 2^2000 is not 041 * representable as a double, but {@code roundToDouble(BigDecimal.valueOf(2).pow(2000), HALF_UP)} 042 * will return {@code Double.MAX_VALUE}, not {@code Double.POSITIVE_INFINITY}. 043 * 044 * <p>For the case of {@link RoundingMode#HALF_EVEN}, this implementation uses the IEEE 754 045 * default rounding mode: if the two nearest representable values are equally near, the one with 046 * the least significant bit zero is chosen. (In such cases, both of the nearest representable 047 * values are even integers; this method returns the one that is a multiple of a greater power of 048 * two.) 049 * 050 * @throws ArithmeticException if {@code mode} is {@link RoundingMode#UNNECESSARY} and {@code x} 051 * is not precisely representable as a {@code double} 052 * @since 30.0 053 */ 054 public static double roundToDouble(BigDecimal x, RoundingMode mode) { 055 return BigDecimalToDoubleRounder.INSTANCE.roundToDouble(x, mode); 056 } 057 058 private static class BigDecimalToDoubleRounder extends ToDoubleRounder<BigDecimal> { 059 static final BigDecimalToDoubleRounder INSTANCE = new BigDecimalToDoubleRounder(); 060 061 private BigDecimalToDoubleRounder() {} 062 063 @Override 064 double roundToDoubleArbitrarily(BigDecimal bigDecimal) { 065 return bigDecimal.doubleValue(); 066 } 067 068 @Override 069 int sign(BigDecimal bigDecimal) { 070 return bigDecimal.signum(); 071 } 072 073 @Override 074 BigDecimal toX(double d, RoundingMode mode) { 075 return new BigDecimal(d); 076 } 077 078 @Override 079 BigDecimal minus(BigDecimal a, BigDecimal b) { 080 return a.subtract(b); 081 } 082 } 083}