From 675f1ba456b30bc7ed6e102709b840524958487d Mon Sep 17 00:00:00 2001 From: Seva Safris Date: Wed, 10 Jun 2020 11:55:21 +0700 Subject: [PATCH] Add LongDecial.MIN_SCALE_BITS and LongDecimal.MAX_SCALE_BITS, re #4 --- src/main/java/org/libj/math/LongDecimal.java | 20 ++++++++++++------- .../java/org/libj/math/LongDecimalTest.java | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/libj/math/LongDecimal.java b/src/main/java/org/libj/math/LongDecimal.java index 17392895..f3505e8b 100644 --- a/src/main/java/org/libj/math/LongDecimal.java +++ b/src/main/java/org/libj/math/LongDecimal.java @@ -28,15 +28,21 @@ */ public final class LongDecimal { private static final Logger logger = LoggerFactory.getLogger(LongDecimal.class); - static final byte maxScaleBits = 17; - private static final long[] pow2 = new long[maxScaleBits]; - static final byte[] minPrecision = new byte[maxScaleBits]; - static final byte[] maxPrecision = new byte[maxScaleBits]; - static final short[] minScale = new short[maxScaleBits]; - static final short[] maxScale = new short[maxScaleBits]; + + /** The minimum allowed number of scale bits (inclusive). */ + public static final byte MIN_SCALE_BITS = 0; + /** The maximum allowed number of scale bits (inclusive). */ + public static final byte MAX_SCALE_BITS = 16; + + private static final byte noScaleBits = MAX_SCALE_BITS + 1; + private static final long[] pow2 = new long[noScaleBits]; + static final byte[] minPrecision = new byte[noScaleBits]; + static final byte[] maxPrecision = new byte[noScaleBits]; + static final short[] minScale = new short[noScaleBits]; + static final short[] maxScale = new short[noScaleBits]; static { - for (byte b = 0; b < maxScaleBits; ++b) { + for (byte b = 0; b < noScaleBits; ++b) { minPrecision[b] = Numbers.precision(minValue(b)); maxPrecision[b] = Numbers.precision(maxValue(b)); pow2[b] = (long)Math.pow(2, b); diff --git a/src/test/java/org/libj/math/LongDecimalTest.java b/src/test/java/org/libj/math/LongDecimalTest.java index 6ef4ca59..ac21ac72 100644 --- a/src/test/java/org/libj/math/LongDecimalTest.java +++ b/src/test/java/org/libj/math/LongDecimalTest.java @@ -51,8 +51,8 @@ public abstract class LongDecimalTest { static final NumberFormat expectedFormatter = new DecimalFormat("0E0"); static final NumberFormat epsilonFormatter = new DecimalFormat("0E0"); static final long[] pow2 = new long[64]; - static final BigInteger[] minValue = new BigInteger[maxScaleBits]; - static final BigInteger[] maxValue = new BigInteger[maxScaleBits]; + static final BigInteger[] minValue = new BigInteger[MAX_SCALE_BITS + 1]; + static final BigInteger[] maxValue = new BigInteger[MAX_SCALE_BITS + 1]; static { for (byte i = 0; i < minValue.length; ++i) {