angle units
This commit is contained in:
parent
a89f6eddef
commit
57309170e8
@ -23,13 +23,11 @@
|
|||||||
a:summary="@string/c_calc_grouping_separator_summary"
|
a:summary="@string/c_calc_grouping_separator_summary"
|
||||||
a:entryValues="@array/p_grouping_separator_values"/>
|
a:entryValues="@array/p_grouping_separator_values"/>
|
||||||
|
|
||||||
<!--
|
|
||||||
<ListPreference a:key="@string/p_calc_angle_units_key"
|
<ListPreference a:key="@string/p_calc_angle_units_key"
|
||||||
a:title="@string/c_calc_angle_units"
|
a:title="@string/c_calc_angle_units"
|
||||||
a:entries="@array/p_angle_units_names"
|
a:entries="@array/p_angle_units_names"
|
||||||
a:summary="@string/c_angle_units_summary"
|
a:summary="@string/c_angle_units_summary"
|
||||||
a:entryValues="@array/p_angle_units"/>
|
a:entryValues="@array/p_angle_units"/>
|
||||||
-->
|
|
||||||
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
@ -37,7 +37,10 @@ import org.solovyev.common.utils.history.HistoryAction;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.text.DecimalFormatSymbols;
|
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 {
|
public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
@ -164,6 +167,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
|||||||
editor.commit();
|
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) {
|
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) ||
|
if (CalculatorEngine.GROUPING_SEPARATOR_P_KEY.equals(key) ||
|
||||||
CalculatorEngine.ROUND_RESULT_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);
|
CalculatorEngine.instance.reset(this, preferences);
|
||||||
this.calculatorModel.evaluate();
|
this.calculatorModel.evaluate();
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ package org.solovyev.android.calculator.model;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import jscl.AngleUnits;
|
||||||
import jscl.JsclMathEngine;
|
import jscl.JsclMathEngine;
|
||||||
import jscl.MathEngine;
|
import jscl.MathEngine;
|
||||||
import jscl.math.function.Function;
|
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_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
|
||||||
public static final String RESULT_PRECISION_DEFAULT = "5";
|
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
|
@NotNull
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
|
|
||||||
@ -55,7 +59,7 @@ public enum CalculatorEngine {
|
|||||||
private int precision = 5;
|
private int precision = 5;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private MathEngine engine = new JsclMathEngine();
|
private MathEngine engine = JsclMathEngine.instance;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public final TextProcessor<PreparedExpression> preprocessor = new ToJsclTextProcessor();
|
public final TextProcessor<PreparedExpression> preprocessor = new ToJsclTextProcessor();
|
||||||
@ -270,6 +274,7 @@ public enum CalculatorEngine {
|
|||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT)));
|
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.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);
|
final String groupingSeparator = preferences.getString(GROUPING_SEPARATOR_P_KEY, GROUPING_SEPARATOR_DEFAULT);
|
||||||
if (StringUtils.isEmpty(groupingSeparator)) {
|
if (StringUtils.isEmpty(groupingSeparator)) {
|
||||||
@ -321,6 +326,10 @@ public enum CalculatorEngine {
|
|||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDefaultAngleUnits(@NotNull AngleUnits angleUnits) {
|
||||||
|
getEngine().setDefaultAngleUnits(angleUnits);
|
||||||
|
}
|
||||||
|
|
||||||
// for tests only
|
// for tests only
|
||||||
void setThreadKiller(@NotNull ThreadKiller threadKiller) {
|
void setThreadKiller(@NotNull ThreadKiller threadKiller) {
|
||||||
this.threadKiller = threadKiller;
|
this.threadKiller = threadKiller;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package org.solovyev.android.calculator.model;
|
package org.solovyev.android.calculator.model;
|
||||||
|
|
||||||
|
import jscl.AngleUnits;
|
||||||
import jscl.math.Expression;
|
import jscl.math.Expression;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.BeforeClass;
|
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.simplify, "eq( 1, 1)").getResult());
|
||||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "lg(10)").getResult());
|
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "lg(10)").getResult());
|
||||||
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2").getResult());
|
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2").getResult());
|
||||||
Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)").getResult());
|
final AngleUnits defaultAngleUnits = cm.getEngine().getDefaultAngleUnits();
|
||||||
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)").getResult());
|
try {
|
||||||
Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)").getResult());
|
cm.getEngine().setDefaultAngleUnits(AngleUnits.rad);
|
||||||
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult());
|
Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)").getResult());
|
||||||
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").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, "e^2").getResult());
|
||||||
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(1)^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("7.389", cm.evaluate(JsclOperation.numeric, "exp(2)").getResult());
|
||||||
Assert.assertEquals("2+i", cm.evaluate(JsclOperation.numeric, "2*1+√(-1)").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("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+iexp(2)").getResult());
|
||||||
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(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 {
|
public void testDegrees() throws Exception {
|
||||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||||
|
|
||||||
cm.setPrecision(3);
|
final AngleUnits defaultAngleUnits = cm.getEngine().getDefaultAngleUnits();
|
||||||
try {
|
try {
|
||||||
Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "°"));
|
cm.getEngine().setDefaultAngleUnits(AngleUnits.rad);
|
||||||
fail();
|
cm.setPrecision(3);
|
||||||
} catch (ParseException e) {
|
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user