Skip to content

Commit 6e2f20d

Browse files
committed
Remove round preference
1 parent 2ac061d commit 6e2f20d

File tree

9 files changed

+42
-52
lines changed

9 files changed

+42
-52
lines changed

app/src/main/java/org/solovyev/android/calculator/Engine.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.solovyev.android.prefs.IntegerPreference;
3838
import org.solovyev.android.prefs.Preference;
3939
import org.solovyev.android.prefs.StringPreference;
40+
import org.solovyev.common.NumberFormatter;
4041
import org.solovyev.common.text.EnumMapper;
4142
import org.solovyev.common.text.NumberMapper;
4243

@@ -93,7 +94,7 @@ public Engine(@Nonnull MathEngine mathEngine, @Nonnull VariablesRegistry variabl
9394
public Engine(@Nonnull JsclMathEngine mathEngine) {
9495
this.mathEngine = mathEngine;
9596

96-
this.mathEngine.setRoundResult(true);
97+
this.mathEngine.setPrecision(5);
9798
this.mathEngine.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
9899
}
99100

@@ -193,13 +194,24 @@ private void checkPreferences() {
193194
final boolean scientific = preferences.getBoolean("engine.output.science_notation", false);
194195
Preferences.Output.notation.putPreference(editor, scientific ? Notation.sci : Notation.dec);
195196
}
196-
migratePreference(preferences, Preferences.Output.round, "org.solovyev.android.calculator.CalculatorModel_round_result", editor);
197+
if (preferences.contains("org.solovyev.android.calculator.CalculatorModel_round_result")) {
198+
final boolean round = preferences.getBoolean("org.solovyev.android.calculator.CalculatorModel_round_result", true);
199+
if (!round) {
200+
Preferences.Output.precision.putPreference(editor, NumberFormatter.MAX_PRECISION);
201+
}
202+
}
197203
} else if (oldVersion == 1) {
198204
migratePreference(preferences, Preferences.Output.separator, "engine.groupingSeparator", editor);
199205
if (preferences.contains("engine.output.scientificNotation")) {
200206
final boolean scientific = preferences.getBoolean("engine.output.scientificNotation", false);
201207
Preferences.Output.notation.putPreference(editor, scientific ? Notation.sci : Notation.dec);
202208
}
209+
if (preferences.contains("engine.output.round")) {
210+
final boolean round = preferences.getBoolean("engine.output.round", true);
211+
if (!round) {
212+
Preferences.Output.precision.putPreference(editor, NumberFormatter.MAX_PRECISION);
213+
}
214+
}
203215
}
204216
Preferences.version.putDefault(editor);
205217
editor.apply();
@@ -228,7 +240,6 @@ private void applyPreferences() {
228240

229241
mathEngine.setPrecision(Preferences.Output.precision.getPreference(preferences));
230242
mathEngine.setNotation(Preferences.Output.notation.getPreference(preferences).id);
231-
mathEngine.setRoundResult(Preferences.Output.round.getPreference(preferences));
232243
mathEngine.setGroupingSeparator(Preferences.Output.separator.getPreference(preferences));
233244

234245
bus.post(ChangedEvent.INSTANCE);
@@ -284,7 +295,6 @@ public static class Preferences {
284295
preferenceKeys.add(numeralBase.getKey());
285296
preferenceKeys.add(angleUnit.getKey());
286297
preferenceKeys.add(Output.precision.getKey());
287-
preferenceKeys.add(Output.round.getKey());
288298
preferenceKeys.add(Output.notation.getKey());
289299
preferenceKeys.add(Output.separator.getKey());
290300
}
@@ -296,7 +306,6 @@ public static List<String> getPreferenceKeys() {
296306

297307
public static class Output {
298308
public static final StringPreference<Integer> precision = StringPreference.ofTypedValue("engine.output.precision", "5", NumberMapper.of(Integer.class));
299-
public static final BooleanPreference round = BooleanPreference.of("engine.output.round", true);
300309
public static final StringPreference<Notation> notation = StringPreference.ofEnum("engine.output.notation", Notation.dec, Notation.class);
301310
public static final CharacterPreference separator = CharacterPreference.of("engine.output.separator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
302311
}

app/src/main/java/org/solovyev/android/calculator/preferences/NumberFormatPreference.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import org.solovyev.android.calculator.R;
1818
import org.solovyev.android.calculator.text.NaturalComparator;
1919
import org.solovyev.android.views.DiscreteSeekBar;
20+
import org.solovyev.common.NumberFormatter;
2021

2122
import butterknife.Bind;
2223
import butterknife.ButterKnife;
23-
import jscl.JsclMathEngine;
2424

2525
import static org.solovyev.android.calculator.Engine.Preferences.Output;
2626

@@ -112,7 +112,7 @@ private ArrayAdapter<Named<Engine.Notation>> makeNotationAdapter() {
112112
private ArrayAdapter<Named<Character>> makeSeparatorAdapter() {
113113
final Context context = getContext();
114114
final ArrayAdapter<Named<Character>> adapter = App.makeSimpleSpinnerAdapter(context);
115-
adapter.add(Named.create(JsclMathEngine.GROUPING_SEPARATOR_NO, R.string.p_grouping_separator_no, context));
115+
adapter.add(Named.create(NumberFormatter.NO_GROUPING, R.string.p_grouping_separator_no, context));
116116
adapter.add(Named.create('\'', R.string.p_grouping_separator_apostrophe, context));
117117
adapter.add(Named.create(' ', R.string.p_grouping_separator_space, context));
118118
return adapter;

app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import android.widget.ListView;
1414

1515
import org.solovyev.android.calculator.AdView;
16-
import org.solovyev.android.calculator.Engine;
1716
import org.solovyev.android.calculator.Preferences;
1817
import org.solovyev.android.calculator.Preferences.Gui.Theme;
1918
import org.solovyev.android.calculator.R;
@@ -135,8 +134,6 @@ public void onError(int i, @Nonnull Exception e) {
135134
});
136135
}
137136
});
138-
139-
onSharedPreferenceChanged(preferences, Engine.Preferences.Output.round.getKey());
140137
}
141138

142139
private void prepareLayoutPreference(int preference) {
@@ -221,12 +218,6 @@ private Checkout getCheckout() {
221218

222219
@Override
223220
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
224-
if (Engine.Preferences.Output.round.getKey().equals(key)) {
225-
final Preference preference = findPreference(Engine.Preferences.Output.precision.getKey());
226-
if (preference != null) {
227-
preference.setEnabled(preferences.getBoolean(key, Engine.Preferences.Output.round.getDefaultValue()));
228-
}
229-
}
230221
}
231222

232223
@Override

app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.solovyev.android.calculator.Engine;
2828
import org.solovyev.android.calculator.Preferences;
29+
import org.solovyev.common.NumberFormatter;
2930

3031
import javax.annotation.Nonnull;
3132

@@ -41,7 +42,7 @@ protected void apply(@Nonnull SharedPreferences preferences) {
4142
Preferences.Gui.mode.putPreference(editor, Preferences.Gui.Mode.simple);
4243
Engine.Preferences.angleUnit.putPreference(editor, AngleUnit.deg);
4344
Engine.Preferences.Output.notation.putPreference(editor, Engine.Notation.dec);
44-
Engine.Preferences.Output.round.putPreference(editor, true);
45+
Engine.Preferences.Output.precision.putPreference(editor, 5);
4546

4647
editor.apply();
4748
}
@@ -55,7 +56,7 @@ protected void apply(@Nonnull SharedPreferences preferences) {
5556
Preferences.Gui.mode.putPreference(editor, Preferences.Gui.Mode.engineer);
5657
Engine.Preferences.angleUnit.putPreference(editor, AngleUnit.rad);
5758
Engine.Preferences.Output.notation.putPreference(editor, Engine.Notation.eng);
58-
Engine.Preferences.Output.round.putPreference(editor, false);
59+
Engine.Preferences.Output.precision.putPreference(editor, NumberFormatter.MAX_PRECISION);
5960

6061
editor.apply();
6162
}

jscl/src/main/java/jscl/JsclMathEngine.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public class JsclMathEngine implements MathEngine {
3636
public static final AngleUnit DEFAULT_ANGLE_UNITS = AngleUnit.deg;
3737
public static final NumeralBase DEFAULT_NUMERAL_BASE = NumeralBase.dec;
3838
public static final char GROUPING_SEPARATOR_DEFAULT = ' ';
39-
public static final char GROUPING_SEPARATOR_NO = 0;
40-
public static final int MAX_FRACTION_DIGITS = 20;
4139
@Nonnull
4240
private static JsclMathEngine instance = new JsclMathEngine();
4341
@Nonnull
@@ -49,10 +47,9 @@ protected NumberFormatter initialValue() {
4947
return new NumberFormatter();
5048
}
5149
};
52-
private char groupingSeparator = GROUPING_SEPARATOR_NO;
53-
private boolean roundResult = false;
50+
private char groupingSeparator = NumberFormatter.NO_GROUPING;
5451
private int notation = FSE_NONE;
55-
private int precision = 5;
52+
private int precision = NumberFormatter.MAX_PRECISION;
5653
@Nonnull
5754
private AngleUnit angleUnits = DEFAULT_ANGLE_UNITS;
5855
@Nonnull
@@ -182,7 +179,7 @@ public String format(double value, @Nonnull NumeralBase nb) {
182179
private NumberFormatter prepareNumberFormatter(@Nonnull NumeralBase nb) {
183180
final NumberFormatter nf = numberFormatter.get();
184181
nf.setGroupingSeparator(hasGroupingSeparator() ? getGroupingSeparatorChar(nb) : NumberFormatter.NO_GROUPING);
185-
nf.setPrecision(roundResult ? precision : NumberFormatter.NO_ROUNDING);
182+
nf.setPrecision(precision);
186183
switch (notation) {
187184
case FSE_ENG:
188185
nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE);
@@ -264,7 +261,7 @@ public String convert(@Nonnull Double value, @Nonnull NumeralBase to) {
264261

265262
ungroupedValue = to.toString(new BigDecimal(value).toBigInteger());
266263
} catch (NotIntegerException e) {
267-
ungroupedValue = to.toString(value, roundResult ? precision : MAX_FRACTION_DIGITS);
264+
ungroupedValue = to.toString(value, precision);
268265
}
269266

270267
return addGroupingSeparators(to, ungroupedValue);
@@ -309,7 +306,7 @@ public String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ung
309306
}
310307

311308
private boolean hasGroupingSeparator() {
312-
return groupingSeparator != JsclMathEngine.GROUPING_SEPARATOR_NO;
309+
return groupingSeparator != NumberFormatter.NO_GROUPING;
313310
}
314311

315312
@Nonnull
@@ -347,10 +344,6 @@ private StringBuilder insertSeparators(@Nonnull NumeralBase nb,
347344
return result;
348345
}
349346

350-
public void setRoundResult(boolean roundResult) {
351-
this.roundResult = roundResult;
352-
}
353-
354347
public void setPrecision(int precision) {
355348
this.precision = precision;
356349
}

jscl/src/main/java/jscl/MathContext.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public interface MathContext {
3838

3939
void setNumeralBase(@Nonnull NumeralBase numeralBase);
4040

41-
void setRoundResult(boolean roundResult);
42-
4341
void setPrecision(int precision);
4442

4543
void setGroupingSeparator(char separator);

jscl/src/main/java/org/solovyev/common/NumberFormatter.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package org.solovyev.common;
22

3-
import midpcalc.Real;
4-
5-
import javax.annotation.Nonnull;
63
import java.math.BigDecimal;
74
import java.math.BigInteger;
85

6+
import javax.annotation.Nonnull;
7+
8+
import midpcalc.Real;
9+
910
import static java.lang.Math.pow;
10-
import static midpcalc.Real.NumberFormat.*;
11+
import static midpcalc.Real.NumberFormat.FSE_ENG;
12+
import static midpcalc.Real.NumberFormat.FSE_FIX;
13+
import static midpcalc.Real.NumberFormat.FSE_NONE;
14+
import static midpcalc.Real.NumberFormat.FSE_SCI;
1115

1216
public class NumberFormatter {
1317

14-
public static final int NO_GROUPING = 0;
18+
public static final char NO_GROUPING = 0;
1519
public static final int NO_ROUNDING = -1;
1620
public static final int DEFAULT_MAGNITUDE = 5;
1721
public static final int MAX_PRECISION = 16;
@@ -94,7 +98,6 @@ public CharSequence format(@Nonnull BigInteger value, int radix) {
9498
final BigInteger absValue = value.abs();
9599
final boolean simpleFormat = useSimpleFormat(radix, absValue);
96100

97-
final int effectivePrecision = precision == NO_ROUNDING ? MAX_PRECISION : precision;
98101
if (simpleFormat) {
99102
numberFormat.fse = FSE_FIX;
100103
} else if (format == FSE_NONE) {
@@ -105,7 +108,7 @@ public CharSequence format(@Nonnull BigInteger value, int radix) {
105108
numberFormat.fse = format;
106109
}
107110
numberFormat.thousand = groupingSeparator;
108-
numberFormat.precision = effectivePrecision;
111+
numberFormat.precision = Math.max(0, Math.min(precision, MAX_PRECISION));
109112
numberFormat.base = radix;
110113
numberFormat.maxwidth = simpleFormat ? 100 : 30;
111114

jscl/src/test/java/jscl/JsclMathEngineTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Before;
44
import org.junit.Test;
5+
import org.solovyev.common.NumberFormatter;
56

67
import midpcalc.Real;
78

@@ -39,7 +40,6 @@ public void testFormat() throws Exception {
3940
assertEquals("1 0100", me.format(20d, NumeralBase.bin));
4041
assertEquals("1 1111", me.format(31d, NumeralBase.bin));
4142

42-
me.setRoundResult(true);
4343
me.setPrecision(10);
4444

4545
assertEquals("111 1111 0011 0110", me.format(32566d, NumeralBase.bin));
@@ -57,11 +57,11 @@ public void testFormat() throws Exception {
5757
assertEquals("D 25 0F 77 0A.6F7319", me.format(56456345354.43534534523459999d, NumeralBase.hex));
5858
assertEquals("3 E7.4CCCCCCCCD", me.format(999.3d, NumeralBase.hex));
5959

60-
me.setRoundResult(false);
60+
me.setPrecision(NumberFormatter.MAX_PRECISION);
6161
assertEquals("6.CCDA6A054226DB6E-19", me.format(0.00000000000000000000009d, NumeralBase.hex));
6262
assertEquals("A.E15D766ED03E2BEE-20", me.format(0.000000000000000000000009d, NumeralBase.hex));
6363
} finally {
64-
me.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_NO);
64+
me.setGroupingSeparator(NumberFormatter.NO_GROUPING);
6565
}
6666

6767
assertEquals("1", me.format(1d, NumeralBase.bin));
@@ -108,7 +108,6 @@ public void testHexShouldAlwaysUseSpaceAsGroupingSeparator() throws Exception {
108108
@Test
109109
public void testEngineeringNotationWithRounding() throws Exception {
110110
me.setNotation(Real.NumberFormat.FSE_ENG);
111-
me.setRoundResult(true);
112111
me.setPrecision(5);
113112

114113
assertEquals("10E6", me.format(10000000d));
@@ -164,7 +163,7 @@ public void testEngineeringNotationWithRounding() throws Exception {
164163
@Test
165164
public void testEngineeringNotationWithoutRounding() throws Exception {
166165
me.setNotation(Real.NumberFormat.FSE_ENG);
167-
me.setRoundResult(false);
166+
me.setPrecision(NumberFormatter.MAX_PRECISION);
168167

169168
assertEquals("10E6", me.format(10000000d));
170169
assertEquals("99E6", me.format(99000000d));

jscl/src/test/java/jscl/math/ExpressionTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jscl.math;
22

33
import org.junit.Test;
4+
import org.solovyev.common.NumberFormatter;
45

56
import java.io.BufferedReader;
67
import java.io.IOException;
@@ -804,20 +805,17 @@ public void testFormat() throws Exception {
804805
assertEquals("100", Expression.valueOf("100.0").simplify().toString());
805806

806807
me.setNotation(Real.NumberFormat.FSE_NONE);
807-
me.setRoundResult(true);
808808
me.setPrecision(5);
809809
assertEquals("0", Expression.valueOf("1222/(10^9)").numeric().toString());
810810

811811
me.setNotation(Real.NumberFormat.FSE_SCI);
812-
me.setRoundResult(true);
813812
me.setPrecision(5);
814813
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
815814

816-
me.setRoundResult(true);
817815
me.setPrecision(10);
818816
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
819817

820-
me.setRoundResult(false);
818+
me.setPrecision(NumberFormatter.MAX_PRECISION);
821819
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
822820

823821
me.setNotation(Real.NumberFormat.FSE_NONE);
@@ -826,19 +824,17 @@ public void testFormat() throws Exception {
826824
me.setNotation(Real.NumberFormat.FSE_SCI);
827825
assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString());
828826

829-
me.setRoundResult(true);
830827
me.setPrecision(10);
831828
assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
832829

833830
me.setNotation(Real.NumberFormat.FSE_NONE);
834-
me.setRoundResult(true);
835831
me.setPrecision(10);
836832
assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
837833

838834
} finally {
839-
me.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_NO);
835+
me.setGroupingSeparator(NumberFormatter.NO_GROUPING);
840836
me.setNotation(Real.NumberFormat.FSE_NONE);
841-
me.setRoundResult(false);
837+
me.setPrecision(NumberFormatter.MAX_PRECISION);
842838
}
843839
}
844840
}

0 commit comments

Comments
 (0)