From 455236a6a3313bf583247471daeed00feb7d3e8d Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 20 Nov 2011 22:53:35 +0400 Subject: [PATCH] angle units --- res/xml/main_preferences.xml | 2 - .../calculator/CalculatorActivity.java | 14 ++++- .../calculator/model/CalculatorEngine.java | 11 +++- .../model/CalculatorEngineTest.java | 62 ++++++++++++------- 4 files changed, 62 insertions(+), 27 deletions(-) diff --git a/res/xml/main_preferences.xml b/res/xml/main_preferences.xml index 1712593d..d4527449 100644 --- a/res/xml/main_preferences.xml +++ b/res/xml/main_preferences.xml @@ -23,13 +23,11 @@ a:summary="@string/c_calc_grouping_separator_summary" a:entryValues="@array/p_grouping_separator_values"/> - diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index ee629396..fb443cd4 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -37,7 +37,10 @@ import org.solovyev.common.utils.history.HistoryAction; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.text.DecimalFormatSymbols; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { @@ -164,6 +167,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh editor.commit(); } } + + if (!preferences.contains(CalculatorEngine.ANGLE_UNITS_P_KEY)) { + final SharedPreferences.Editor editor = preferences.edit(); + editor.putString(CalculatorEngine.ANGLE_UNITS_P_KEY, CalculatorEngine.ANGLE_UNITS_DEFAULT); + editor.commit(); + } } private synchronized void setOnDragListeners(@NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) { @@ -491,7 +500,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh if (CalculatorEngine.GROUPING_SEPARATOR_P_KEY.equals(key) || CalculatorEngine.ROUND_RESULT_P_KEY.equals(key) || - CalculatorEngine.RESULT_PRECISION_P_KEY.equals(key)) { + CalculatorEngine.RESULT_PRECISION_P_KEY.equals(key) || + CalculatorEngine.ANGLE_UNITS_P_KEY.equals(key)) { CalculatorEngine.instance.reset(this, preferences); this.calculatorModel.evaluate(); } diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index 06dd60cd..19565aeb 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -7,6 +7,7 @@ package org.solovyev.android.calculator.model; import android.content.Context; import android.content.SharedPreferences; +import jscl.AngleUnits; import jscl.JsclMathEngine; import jscl.MathEngine; import jscl.math.function.Function; @@ -47,6 +48,9 @@ public enum CalculatorEngine { public static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision"; public static final String RESULT_PRECISION_DEFAULT = "5"; + public static final String ANGLE_UNITS_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_angle_units"; + public static final String ANGLE_UNITS_DEFAULT = "deg"; + @NotNull private final Object lock = new Object(); @@ -55,7 +59,7 @@ public enum CalculatorEngine { private int precision = 5; @NotNull - private MathEngine engine = new JsclMathEngine(); + private MathEngine engine = JsclMathEngine.instance; @NotNull public final TextProcessor preprocessor = new ToJsclTextProcessor(); @@ -270,6 +274,7 @@ public enum CalculatorEngine { //noinspection ConstantConditions this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT))); this.setRoundResult(preferences.getBoolean(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT)); + this.setDefaultAngleUnits(AngleUnits.valueOf(preferences.getString(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT))); final String groupingSeparator = preferences.getString(GROUPING_SEPARATOR_P_KEY, GROUPING_SEPARATOR_DEFAULT); if (StringUtils.isEmpty(groupingSeparator)) { @@ -321,6 +326,10 @@ public enum CalculatorEngine { this.timeout = timeout; } + public void setDefaultAngleUnits(@NotNull AngleUnits angleUnits) { + getEngine().setDefaultAngleUnits(angleUnits); + } + // for tests only void setThreadKiller(@NotNull ThreadKiller threadKiller) { this.threadKiller = threadKiller; diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index b928e64a..b9987887 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -5,6 +5,7 @@ package org.solovyev.android.calculator.model; +import jscl.AngleUnits; import jscl.math.Expression; import org.junit.Assert; import org.junit.BeforeClass; @@ -84,16 +85,27 @@ public class CalculatorEngineTest { Assert.assertEquals("1", cm.evaluate(JsclOperation.simplify, "eq( 1, 1)").getResult()); Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "lg(10)").getResult()); Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2").getResult()); - Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)").getResult()); - Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)").getResult()); - Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)").getResult()); - Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult()); - Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult()); + final AngleUnits defaultAngleUnits = cm.getEngine().getDefaultAngleUnits(); + try { + cm.getEngine().setDefaultAngleUnits(AngleUnits.rad); + Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)").getResult()); + Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)").getResult()); + Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)").getResult()); + Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult()); + Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult()); + } finally { + cm.getEngine().setDefaultAngleUnits(defaultAngleUnits); + } Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "e^2").getResult()); Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(1)^2").getResult()); Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(2)").getResult()); Assert.assertEquals("2+i", cm.evaluate(JsclOperation.numeric, "2*1+√(-1)").getResult()); - Assert.assertEquals("0.921+3.142i", cm.evaluate(JsclOperation.numeric, "ln(5cosh(38π√(2cos(2))))").getResult()); + try { + cm.getEngine().setDefaultAngleUnits(AngleUnits.rad); + Assert.assertEquals("0.921+3.142i", cm.evaluate(JsclOperation.numeric, "ln(5cosh(38π√(2cos(2))))").getResult()); + } finally { + cm.getEngine().setDefaultAngleUnits(defaultAngleUnits); + } Assert.assertEquals("7.389i", cm.evaluate(JsclOperation.numeric, "iexp(2)").getResult()); Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+iexp(2)").getResult()); Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(2)").getResult()); @@ -296,24 +308,30 @@ public class CalculatorEngineTest { public void testDegrees() throws Exception { final CalculatorEngine cm = CalculatorEngine.instance; - cm.setPrecision(3); + final AngleUnits defaultAngleUnits = cm.getEngine().getDefaultAngleUnits(); try { - Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "°")); - fail(); - } catch (ParseException e) { + cm.getEngine().setDefaultAngleUnits(AngleUnits.rad); + cm.setPrecision(3); + try { + Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "°")); + fail(); + } catch (ParseException e) { + } + + Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "1°").getResult()); + Assert.assertEquals("0.349", cm.evaluate(JsclOperation.numeric, "20.0°").getResult()); + Assert.assertEquals("0.5", cm.evaluate(JsclOperation.numeric, "sin(30°)").getResult()); + Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(sin(30°))").getResult()); + try { + Assert.assertEquals("∂(cos(t), t, t,1°)", cm.evaluate(JsclOperation.numeric, "∂(cos(t),t,t,1°)").getResult()); + fail(); + } catch (ParseException e) { + } + + Assert.assertEquals("∂(cos(t), t, t,1°)", cm.evaluate(JsclOperation.simplify, "∂(cos(t),t,t,1°)").getResult()); + } finally { + cm.getEngine().setDefaultAngleUnits(defaultAngleUnits); } - - Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "1°").getResult()); - Assert.assertEquals("0.349", cm.evaluate(JsclOperation.numeric, "20.0°").getResult()); - Assert.assertEquals("0.5", cm.evaluate(JsclOperation.numeric, "sin(30°)").getResult()); - Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(sin(30°))").getResult()); - try { - Assert.assertEquals("∂(cos(t), t, t,1°)", cm.evaluate(JsclOperation.numeric, "∂(cos(t),t,t,1°)").getResult()); - fail(); - } catch (ParseException e) { - } - - Assert.assertEquals("∂(cos(t), t, t,1°)", cm.evaluate(JsclOperation.simplify, "∂(cos(t),t,t,1°)").getResult()); } }