angle units

This commit is contained in:
serso 2011-11-20 22:53:35 +04:00
parent 5be6a82955
commit 30c76c4af8
4 changed files with 62 additions and 27 deletions

View File

@ -23,13 +23,11 @@
a:summary="@string/c_calc_grouping_separator_summary"
a:entryValues="@array/p_grouping_separator_values"/>
<!--
<ListPreference a:key="@string/p_calc_angle_units_key"
a:title="@string/c_calc_angle_units"
a:entries="@array/p_angle_units_names"
a:summary="@string/c_angle_units_summary"
a:entryValues="@array/p_angle_units"/>
-->
</PreferenceCategory>

View File

@ -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();
}

View File

@ -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<PreparedExpression> 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;

View File

@ -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, "").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, "").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());
}
}