001/*
002 * The MIT License (MIT)
003 *
004 * Copyright (c) 2015-2023 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.Scale0f;
030import org.decimal4j.truncate.DecimalRounding;
031import org.decimal4j.truncate.UncheckedRounding;
032
033/**
034 * Arithmetic implementation without rounding for the special case with
035 * {@link Scale0f}, that is, for longs. If an operation leads to an overflow the
036 * result is silently truncated.
037 */
038public final class UncheckedScale0fTruncatingArithmetic extends AbstractUncheckedScale0fArithmetic {
039
040        /**
041         * The singleton instance.
042         */
043        public static final UncheckedScale0fTruncatingArithmetic INSTANCE = new UncheckedScale0fTruncatingArithmetic();
044
045        @Override
046        public final RoundingMode getRoundingMode() {
047                return RoundingMode.DOWN;
048        }
049        
050        @Override
051        public final UncheckedRounding getTruncationPolicy() {
052                return UncheckedRounding.DOWN;
053        }
054
055        @Override
056        public final long addUnscaled(long uDecimal, long unscaled, int scale) {
057                return Add.addLongUnscaled(uDecimal, unscaled, scale);
058        }
059
060        @Override
061        public final long subtractUnscaled(long uDecimal, long unscaled, int scale) {
062                return Sub.subtractLongUnscaled(uDecimal, unscaled, scale);
063        }
064
065        @Override
066        public final long multiplyByUnscaled(long uDecimal, long unscaled, int scale) {
067                return Mul.multiplyByUnscaled(uDecimal, unscaled, scale);
068        }
069
070        @Override
071        public final long divide(long uDecimalDividend, long uDecimalDivisor) {
072                return uDecimalDividend / uDecimalDivisor;
073        }
074
075        @Override
076        public final long divideByLong(long uDecimalDividend, long lDivisor) {
077                return uDecimalDividend / lDivisor;
078        }
079        
080        @Override
081        public final long divideByUnscaled(long uDecimal, long unscaled, int scale) {
082                return Div.divideByUnscaled(uDecimal, unscaled, scale);
083        }
084
085        @Override
086        public final long multiplyByPowerOf10(long uDecimal, int positions) {
087                return Pow10.multiplyByPowerOf10(uDecimal, positions);
088        }
089
090        @Override
091        public final long divideByPowerOf10(long uDecimal, int positions) {
092                return Pow10.divideByPowerOf10(uDecimal, positions);
093        }
094
095        @Override
096        public final long invert(long uDecimal) {
097                return Invert.invertLong(uDecimal);
098        }
099
100        @Override
101        public final long sqrt(long uDecimal) {
102                return Sqrt.sqrtLong(uDecimal);
103        }
104
105        @Override
106        public final long pow(long uDecimal, int exponent) {
107                return Pow.powLong(this, DecimalRounding.DOWN, uDecimal, exponent);
108        }
109
110        @Override
111        public final long shiftLeft(long uDecimal, int positions) {
112                return Shift.shiftLeft(DecimalRounding.DOWN, uDecimal, positions);
113        }
114
115        @Override
116        public final long shiftRight(long uDecimal, int positions) {
117                return Shift.shiftRight(DecimalRounding.DOWN, uDecimal, positions);
118        }
119
120        @Override
121        public final long avg(long a, long b) {
122                return Avg.avg(a, b);
123        }
124
125        @Override
126        public final long round(long uDecimal, int precision) {
127                return Round.round(this, uDecimal, precision);
128        }
129
130        @Override
131        public final long toUnscaled(long uDecimal, int scale) {
132                return UnscaledConversion.unscaledToUnscaled(scale, this, uDecimal);
133        }
134
135        @Override
136        public final double toDouble(long uDecimal) {
137                return DoubleConversion.longToDouble(this, uDecimal);
138        }
139
140        @Override
141        public final float toFloat(long uDecimal) {
142                return FloatConversion.longToFloat(this, uDecimal);
143        }
144
145        @Override
146        public final long fromUnscaled(long unscaledValue, int scale) {
147                return UnscaledConversion.unscaledToLong(this, unscaledValue, scale);
148        }
149
150        @Override
151        public final long fromFloat(float value) {
152                return FloatConversion.floatToLong(value);
153        }
154
155        @Override
156        public final long fromDouble(double value) {
157                return DoubleConversion.doubleToLong(value);
158        }
159
160        @Override
161        public final long fromBigDecimal(BigDecimal value) {
162                return BigDecimalConversion.bigDecimalToLong(RoundingMode.DOWN, value);
163        }
164
165        @Override
166        public final long parse(String value) {
167                return StringConversion.parseLong(this, DecimalRounding.DOWN, value, 0, value.length());
168        }
169        
170        @Override
171        public final long parse(CharSequence value, int start, int end) {
172                return StringConversion.parseLong(this, DecimalRounding.DOWN, value, start, end);
173        }
174
175}