001/** 002 * The MIT License (MIT) 003 * 004 * Copyright (c) 2015-2016 decimal4j (tools4j), Marco Terzer 005 * 006 * Permission is hereby granted, free of charge, to any person obtaining a copy 007 * of this software and associated documentation files (the "Software"), to deal 008 * in the Software without restriction, including without limitation the rights 009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010 * copies of the Software, and to permit persons to whom the Software is 011 * furnished to do so, subject to the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be included in all 014 * copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 022 * SOFTWARE. 023 */ 024package org.decimal4j.arithmetic; 025 026import java.math.BigDecimal; 027import java.math.RoundingMode; 028 029import org.decimal4j.scale.ScaleMetrics; 030import org.decimal4j.truncate.CheckedRounding; 031import org.decimal4j.truncate.DecimalRounding; 032 033/** 034 * Arithmetic implementation without rounding but with overflow check for scales 035 * other than zero. An exception is thrown if an operation leads to an overflow. 036 */ 037public final class CheckedScaleNfTruncatingArithmetic extends AbstractCheckedScaleNfArithmetic { 038 039 /** 040 * Constructor with scale metrics for this arithmetic. 041 * 042 * @param scaleMetrics 043 * the scale metrics 044 */ 045 public CheckedScaleNfTruncatingArithmetic(ScaleMetrics scaleMetrics) { 046 super(scaleMetrics); 047 } 048 049 @Override 050 public final RoundingMode getRoundingMode() { 051 return RoundingMode.DOWN; 052 } 053 054 @Override 055 public final CheckedRounding getTruncationPolicy() { 056 return CheckedRounding.DOWN; 057 } 058 059 @Override 060 public final long addUnscaled(long uDecimal, long unscaled, int scale) { 061 return Add.addUnscaledUnscaledChecked(this, uDecimal, unscaled, scale); 062 } 063 064 @Override 065 public final long subtractUnscaled(long uDecimal, long unscaled, int scale) { 066 return Sub.subtractUnscaledUnscaledChecked(this, uDecimal, unscaled, scale); 067 } 068 069 @Override 070 public final long multiplyByUnscaled(long uDecimal, long unscaled, int scale) { 071 return Mul.multiplyByUnscaledChecked(this, uDecimal, unscaled, scale); 072 } 073 074 @Override 075 public final long divideByUnscaled(long uDecimal, long unscaled, int scale) { 076 return Div.divideByUnscaledChecked(this, uDecimal, unscaled, scale); 077 } 078 079 @Override 080 public final long multiply(long uDecimal1, long uDecimal2) { 081 return Mul.multiplyChecked(this, uDecimal1, uDecimal2); 082 } 083 084 @Override 085 public final long square(long uDecimal) { 086 return Square.squareChecked(this, uDecimal); 087 } 088 089 @Override 090 public final long divide(long uDecimalDividend, long uDecimalDivisor) { 091 return Div.divideChecked(this, uDecimalDividend, uDecimalDivisor); 092 } 093 094 @Override 095 public final long pow(long uDecimal, int exponent) { 096 return Pow.pow(this, DecimalRounding.DOWN, uDecimal, exponent); 097 } 098 099 @Override 100 public final long avg(long a, long b) { 101 return Avg.avg(a, b); 102 } 103 104 @Override 105 public final long sqrt(long uDecimal) { 106 return Sqrt.sqrt(this, uDecimal); 107 } 108 109 @Override 110 public final long divideByLong(long uDecimalDividend, long lDivisor) { 111 return Checked.divideByLong(this, uDecimalDividend, lDivisor); 112 } 113 114 @Override 115 public final long divideByPowerOf10(long uDecimal, int positions) { 116 return Pow10.divideByPowerOf10Checked(this, uDecimal, positions); 117 } 118 119 @Override 120 public final long invert(long uDecimal) { 121 return Invert.invert(this, uDecimal); 122 } 123 124 @Override 125 public final long multiplyByPowerOf10(long uDecimal, int positions) { 126 return Pow10.multiplyByPowerOf10Checked(this, uDecimal, positions); 127 } 128 129 @Override 130 public final long shiftLeft(long uDecimal, int positions) { 131 return Shift.shiftLeftChecked(this, DecimalRounding.DOWN, uDecimal, positions); 132 } 133 134 @Override 135 public final long shiftRight(long uDecimal, int positions) { 136 return Shift.shiftRightChecked(this, DecimalRounding.DOWN, uDecimal, positions); 137 } 138 139 @Override 140 public final long round(long uDecimal, int precision) { 141 return Round.round(this, uDecimal, precision); 142 } 143 144 @Override 145 public final long fromLong(long value) { 146 return LongConversion.longToUnscaled(getScaleMetrics(), value); 147 } 148 149 @Override 150 public final long fromFloat(float value) { 151 return FloatConversion.floatToUnscaled(this, DecimalRounding.DOWN, value); 152 } 153 154 @Override 155 public final long fromDouble(double value) { 156 return DoubleConversion.doubleToUnscaled(this, DecimalRounding.DOWN, value); 157 } 158 159 @Override 160 public final long fromUnscaled(long unscaledValue, int scale) { 161 return UnscaledConversion.unscaledToUnscaled(this, unscaledValue, scale); 162 } 163 164 @Override 165 public final long fromBigDecimal(BigDecimal value) { 166 return BigDecimalConversion.bigDecimalToUnscaled(getScaleMetrics(), RoundingMode.DOWN, value); 167 } 168 169 @Override 170 public final long toLong(long uDecimal) { 171 return LongConversion.unscaledToLong(getScaleMetrics(), uDecimal); 172 } 173 174 @Override 175 public final float toFloat(long uDecimal) { 176 return FloatConversion.unscaledToFloat(this, uDecimal); 177 } 178 179 @Override 180 public final double toDouble(long uDecimal) { 181 return DoubleConversion.unscaledToDouble(this, uDecimal); 182 } 183 184 @Override 185 public final long toUnscaled(long uDecimal, int scale) { 186 return UnscaledConversion.unscaledToUnscaled(scale, this, uDecimal); 187 } 188 189 @Override 190 public final long parse(String value) { 191 return StringConversion.parseUnscaledDecimal(this, DecimalRounding.DOWN, value, 0, value.length()); 192 } 193 194 @Override 195 public final long parse(CharSequence value, int start, int end) { 196 return StringConversion.parseUnscaledDecimal(this, DecimalRounding.DOWN, value, start, end); 197 } 198 199}