Remove round preference

This commit is contained in:
serso
2016-05-02 17:16:56 +02:00
parent 2ac061d8ba
commit 6e2f20dd01
9 changed files with 42 additions and 52 deletions

View File

@@ -36,8 +36,6 @@ public class JsclMathEngine implements MathEngine {
public static final AngleUnit DEFAULT_ANGLE_UNITS = AngleUnit.deg;
public static final NumeralBase DEFAULT_NUMERAL_BASE = NumeralBase.dec;
public static final char GROUPING_SEPARATOR_DEFAULT = ' ';
public static final char GROUPING_SEPARATOR_NO = 0;
public static final int MAX_FRACTION_DIGITS = 20;
@Nonnull
private static JsclMathEngine instance = new JsclMathEngine();
@Nonnull
@@ -49,10 +47,9 @@ public class JsclMathEngine implements MathEngine {
return new NumberFormatter();
}
};
private char groupingSeparator = GROUPING_SEPARATOR_NO;
private boolean roundResult = false;
private char groupingSeparator = NumberFormatter.NO_GROUPING;
private int notation = FSE_NONE;
private int precision = 5;
private int precision = NumberFormatter.MAX_PRECISION;
@Nonnull
private AngleUnit angleUnits = DEFAULT_ANGLE_UNITS;
@Nonnull
@@ -182,7 +179,7 @@ public class JsclMathEngine implements MathEngine {
private NumberFormatter prepareNumberFormatter(@Nonnull NumeralBase nb) {
final NumberFormatter nf = numberFormatter.get();
nf.setGroupingSeparator(hasGroupingSeparator() ? getGroupingSeparatorChar(nb) : NumberFormatter.NO_GROUPING);
nf.setPrecision(roundResult ? precision : NumberFormatter.NO_ROUNDING);
nf.setPrecision(precision);
switch (notation) {
case FSE_ENG:
nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE);
@@ -264,7 +261,7 @@ public class JsclMathEngine implements MathEngine {
ungroupedValue = to.toString(new BigDecimal(value).toBigInteger());
} catch (NotIntegerException e) {
ungroupedValue = to.toString(value, roundResult ? precision : MAX_FRACTION_DIGITS);
ungroupedValue = to.toString(value, precision);
}
return addGroupingSeparators(to, ungroupedValue);
@@ -309,7 +306,7 @@ public class JsclMathEngine implements MathEngine {
}
private boolean hasGroupingSeparator() {
return groupingSeparator != JsclMathEngine.GROUPING_SEPARATOR_NO;
return groupingSeparator != NumberFormatter.NO_GROUPING;
}
@Nonnull
@@ -347,10 +344,6 @@ public class JsclMathEngine implements MathEngine {
return result;
}
public void setRoundResult(boolean roundResult) {
this.roundResult = roundResult;
}
public void setPrecision(int precision) {
this.precision = precision;
}

View File

@@ -38,8 +38,6 @@ public interface MathContext {
void setNumeralBase(@Nonnull NumeralBase numeralBase);
void setRoundResult(boolean roundResult);
void setPrecision(int precision);
void setGroupingSeparator(char separator);

View File

@@ -1,17 +1,21 @@
package org.solovyev.common;
import midpcalc.Real;
import javax.annotation.Nonnull;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.annotation.Nonnull;
import midpcalc.Real;
import static java.lang.Math.pow;
import static midpcalc.Real.NumberFormat.*;
import static midpcalc.Real.NumberFormat.FSE_ENG;
import static midpcalc.Real.NumberFormat.FSE_FIX;
import static midpcalc.Real.NumberFormat.FSE_NONE;
import static midpcalc.Real.NumberFormat.FSE_SCI;
public class NumberFormatter {
public static final int NO_GROUPING = 0;
public static final char NO_GROUPING = 0;
public static final int NO_ROUNDING = -1;
public static final int DEFAULT_MAGNITUDE = 5;
public static final int MAX_PRECISION = 16;
@@ -94,7 +98,6 @@ public class NumberFormatter {
final BigInteger absValue = value.abs();
final boolean simpleFormat = useSimpleFormat(radix, absValue);
final int effectivePrecision = precision == NO_ROUNDING ? MAX_PRECISION : precision;
if (simpleFormat) {
numberFormat.fse = FSE_FIX;
} else if (format == FSE_NONE) {
@@ -105,7 +108,7 @@ public class NumberFormatter {
numberFormat.fse = format;
}
numberFormat.thousand = groupingSeparator;
numberFormat.precision = effectivePrecision;
numberFormat.precision = Math.max(0, Math.min(precision, MAX_PRECISION));
numberFormat.base = radix;
numberFormat.maxwidth = simpleFormat ? 100 : 30;

View File

@@ -2,6 +2,7 @@ package jscl;
import org.junit.Before;
import org.junit.Test;
import org.solovyev.common.NumberFormatter;
import midpcalc.Real;
@@ -39,7 +40,6 @@ public class JsclMathEngineTest {
assertEquals("1 0100", me.format(20d, NumeralBase.bin));
assertEquals("1 1111", me.format(31d, NumeralBase.bin));
me.setRoundResult(true);
me.setPrecision(10);
assertEquals("111 1111 0011 0110", me.format(32566d, NumeralBase.bin));
@@ -57,11 +57,11 @@ public class JsclMathEngineTest {
assertEquals("D 25 0F 77 0A.6F7319", me.format(56456345354.43534534523459999d, NumeralBase.hex));
assertEquals("3 E7.4CCCCCCCCD", me.format(999.3d, NumeralBase.hex));
me.setRoundResult(false);
me.setPrecision(NumberFormatter.MAX_PRECISION);
assertEquals("6.CCDA6A054226DB6E-19", me.format(0.00000000000000000000009d, NumeralBase.hex));
assertEquals("A.E15D766ED03E2BEE-20", me.format(0.000000000000000000000009d, NumeralBase.hex));
} finally {
me.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_NO);
me.setGroupingSeparator(NumberFormatter.NO_GROUPING);
}
assertEquals("1", me.format(1d, NumeralBase.bin));
@@ -108,7 +108,6 @@ public class JsclMathEngineTest {
@Test
public void testEngineeringNotationWithRounding() throws Exception {
me.setNotation(Real.NumberFormat.FSE_ENG);
me.setRoundResult(true);
me.setPrecision(5);
assertEquals("10E6", me.format(10000000d));
@@ -164,7 +163,7 @@ public class JsclMathEngineTest {
@Test
public void testEngineeringNotationWithoutRounding() throws Exception {
me.setNotation(Real.NumberFormat.FSE_ENG);
me.setRoundResult(false);
me.setPrecision(NumberFormatter.MAX_PRECISION);
assertEquals("10E6", me.format(10000000d));
assertEquals("99E6", me.format(99000000d));

View File

@@ -1,6 +1,7 @@
package jscl.math;
import org.junit.Test;
import org.solovyev.common.NumberFormatter;
import java.io.BufferedReader;
import java.io.IOException;
@@ -804,20 +805,17 @@ public class ExpressionTest {
assertEquals("100", Expression.valueOf("100.0").simplify().toString());
me.setNotation(Real.NumberFormat.FSE_NONE);
me.setRoundResult(true);
me.setPrecision(5);
assertEquals("0", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setNotation(Real.NumberFormat.FSE_SCI);
me.setRoundResult(true);
me.setPrecision(5);
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setRoundResult(true);
me.setPrecision(10);
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setRoundResult(false);
me.setPrecision(NumberFormatter.MAX_PRECISION);
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setNotation(Real.NumberFormat.FSE_NONE);
@@ -826,19 +824,17 @@ public class ExpressionTest {
me.setNotation(Real.NumberFormat.FSE_SCI);
assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString());
me.setRoundResult(true);
me.setPrecision(10);
assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
me.setNotation(Real.NumberFormat.FSE_NONE);
me.setRoundResult(true);
me.setPrecision(10);
assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
} finally {
me.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_NO);
me.setGroupingSeparator(NumberFormatter.NO_GROUPING);
me.setNotation(Real.NumberFormat.FSE_NONE);
me.setRoundResult(false);
me.setPrecision(NumberFormatter.MAX_PRECISION);
}
}
}