scientific notation format

This commit is contained in:
Sergey Solovyev 2012-02-12 00:38:14 +04:00
parent b7f7e82634
commit 8b23c532da
6 changed files with 78 additions and 57 deletions

View File

@ -20,6 +20,7 @@
<string name="c_prefs_main_category">Основные настройки</string> <string name="c_prefs_main_category">Основные настройки</string>
<string name="c_prefs_calculations_category">Настройки вычислений</string> <string name="c_prefs_calculations_category">Настройки вычислений</string>
<string name="c_prefs_appearance_category">Настройки внешнего вида</string> <string name="c_prefs_appearance_category">Настройки внешнего вида</string>
<string name="c_prefs_other_category">Другие настройки</string>
<string name="c_prefs_drag_button_category">Настройки кнопок</string> <string name="c_prefs_drag_button_category">Настройки кнопок</string>
@ -41,6 +42,8 @@
<string name="c_calc_color_display_title">Подсветка выражений</string> <string name="c_calc_color_display_title">Подсветка выражений</string>
<string name="c_calc_round_result_title">Округление результата</string> <string name="c_calc_round_result_title">Округление результата</string>
<string name="c_calc_round_result_summary">Включает/выключает округление результата</string> <string name="c_calc_round_result_summary">Включает/выключает округление результата</string>
<string name="c_calc_science_notation_title">Результат в научной форме</string>
<string name="c_calc_science_notation_summary">Если включено - результат будет всегда записан в научной форме (12.34E-12)</string>
<string name="p_calc_result_precision_title">Точность результата</string> <string name="p_calc_result_precision_title">Точность результата</string>
<string name="c_exit">Выход</string> <string name="c_exit">Выход</string>
<string name="c_add">Добавить</string> <string name="c_add">Добавить</string>

View File

@ -33,6 +33,9 @@
<string name="p_calc_round_result_key">org.solovyev.android.calculator.CalculatorModel_round_result</string> <string name="p_calc_round_result_key">org.solovyev.android.calculator.CalculatorModel_round_result</string>
<string name="p_calc_round_result">true</string> <string name="p_calc_round_result">true</string>
<string name="p_calc_science_notation_key">calculation.output.science_notation</string>
<string name="p_calc_science_notation">false</string>
<string name="p_calc_functions">org.solovyev.android.calculator.CalculatorModel_functions</string> <string name="p_calc_functions">org.solovyev.android.calculator.CalculatorModel_functions</string>
<string name="p_calc_vars">org.solovyev.android.calculator.CalculatorModel_vars</string> <string name="p_calc_vars">org.solovyev.android.calculator.CalculatorModel_vars</string>
<string name="p_calc_history">org.solovyev.android.calculator.CalculatorModel_history</string> <string name="p_calc_history">org.solovyev.android.calculator.CalculatorModel_history</string>

View File

@ -20,6 +20,7 @@
<string name="c_prefs_main_category">Main settings</string> <string name="c_prefs_main_category">Main settings</string>
<string name="c_prefs_calculations_category">Calculation settings</string> <string name="c_prefs_calculations_category">Calculation settings</string>
<string name="c_prefs_appearance_category">Appearance settings</string> <string name="c_prefs_appearance_category">Appearance settings</string>
<string name="c_prefs_other_category">Other settings</string>
<string name="c_prefs_drag_button_category">Drag buttons settings</string> <string name="c_prefs_drag_button_category">Drag buttons settings</string>
@ -41,6 +42,8 @@
<string name="c_calc_color_display_title">Highlight expressions</string> <string name="c_calc_color_display_title">Highlight expressions</string>
<string name="c_calc_round_result_title">Round result</string> <string name="c_calc_round_result_title">Round result</string>
<string name="c_calc_round_result_summary">Toggles rounding of the result</string> <string name="c_calc_round_result_summary">Toggles rounding of the result</string>
<string name="c_calc_science_notation_title">Always scientific notation</string>
<string name="c_calc_science_notation_summary">If turned on forces to use only scientific notation of output (12.34E-12)</string>
<string name="p_calc_result_precision_title">Precision of result</string> <string name="p_calc_result_precision_title">Precision of result</string>
<string name="c_exit">Exit</string> <string name="c_exit">Exit</string>
<string name="c_add">Add</string> <string name="c_add">Add</string>

View File

@ -8,13 +8,13 @@
a:title="@string/c_calc_ad_free_title" a:title="@string/c_calc_ad_free_title"
a:summary="@string/c_calc_ad_free_summary" a:summary="@string/c_calc_ad_free_summary"
a:defaultValue="false" a:defaultValue="false"
/> />
<PreferenceCategory a:title="@string/c_prefs_calculations_category"> <org.solovyev.android.ads.AdViewPreference
a:key="admob_01"
a:layout="@layout/admob_pref"/>
<org.solovyev.android.ads.AdViewPreference <PreferenceScreen a:title="@string/c_prefs_calculations_category">
a:key="admob_01"
a:layout="@layout/admob_pref"/>
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:key="@string/p_calc_round_result_key" a:key="@string/p_calc_round_result_key"
@ -29,18 +29,18 @@
a:defaultValue="5" a:defaultValue="5"
range:boundaries="0;16"/> range:boundaries="0;16"/>
<android.preference.CheckBoxPreference
a:key="@string/p_calc_science_notation_key"
a:summary="@string/c_calc_science_notation_summary"
a:title="@string/c_calc_science_notation_title"
a:defaultValue="false"/>
<ListPreference a:key="@string/p_calc_grouping_separator_key" <ListPreference a:key="@string/p_calc_grouping_separator_key"
a:title="@string/c_calc_grouping_separator" a:title="@string/c_calc_grouping_separator"
a:entries="@array/p_grouping_separator_names" a:entries="@array/p_grouping_separator_names"
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_multiplication_sign_key"
a:title="@string/c_calc_multiplication_sign"
a:entries="@array/p_multiplication_sign_values"
a:summary="@string/c_calc_multiplication_sign_summary"
a:entryValues="@array/p_multiplication_sign_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"
@ -53,14 +53,10 @@
a:summary="@string/c_numeral_bases_summary" a:summary="@string/c_numeral_bases_summary"
a:entryValues="@array/p_numeral_bases"/> a:entryValues="@array/p_numeral_bases"/>
</PreferenceCategory> </PreferenceScreen>
<PreferenceScreen
<PreferenceCategory a:title="@string/c_prefs_appearance_category"> a:title="@string/c_prefs_appearance_category">
<org.solovyev.android.ads.AdViewPreference
a:key="admob_02"
a:layout="@layout/admob_pref"/>
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:key="@string/p_calc_color_display_key" a:key="@string/p_calc_color_display_key"
@ -80,6 +76,12 @@
a:summary="@string/p_calc_haptic_feedback_duration_summary" a:summary="@string/p_calc_haptic_feedback_duration_summary"
a:entryValues="@array/p_calc_haptic_feedback_duration_values"/> a:entryValues="@array/p_calc_haptic_feedback_duration_values"/>
<ListPreference a:key="@string/p_calc_multiplication_sign_key"
a:title="@string/c_calc_multiplication_sign"
a:entries="@array/p_multiplication_sign_values"
a:summary="@string/c_calc_multiplication_sign_summary"
a:entryValues="@array/p_multiplication_sign_values"/>
<ListPreference a:key="@string/p_calc_theme_key" <ListPreference a:key="@string/p_calc_theme_key"
a:title="@string/c_calc_theme" a:title="@string/c_calc_theme"
a:entries="@array/p_theme_names" a:entries="@array/p_theme_names"
@ -102,6 +104,26 @@
range:steps="10" range:steps="10"
range:boundaries="10;500"/> range:boundaries="10;500"/>
<!-- <org.solovyev.android.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_duration_key"
a:title="Duration of drag event"
a:text=" ms"
a:defaultValue="40;2500"
range:steps="10"
range:boundaries="5;4000"/>
<org.solovyev.android.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_angle_key"
a:title="Angle of drag event"
a:text=" degrees"
a:defaultValue="0;45"
range:steps="5"
range:boundaries="0;45"/>-->
</PreferenceScreen>
<PreferenceScreen
a:title="@string/c_prefs_other_category">
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:key="@string/p_calc_show_release_notes_key" a:key="@string/p_calc_show_release_notes_key"
a:summary="@string/c_calc_show_release_notes_summary" a:summary="@string/c_calc_show_release_notes_summary"
@ -114,21 +136,6 @@
a:title="@string/c_calc_use_back_button_as_prev_title" a:title="@string/c_calc_use_back_button_as_prev_title"
a:defaultValue="false"/> a:defaultValue="false"/>
<!-- <org.solovyev.android.prefs.FloatRangeSeekBarPreference </PreferenceScreen>
a:key="@string/p_drag_duration_key"
a:title="Duration of drag event"
a:text=" ms"
a:defaultValue="40;2500"
range:steps="10"
range:boundaries="5;4000"/>
<org.solovyev.android.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_angle_key"
a:title="Angle of drag event"
a:text=" degrees"
a:defaultValue="0;45"
range:steps="5"
range:boundaries="0;45"/>-->
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@ -47,6 +47,9 @@ public enum CalculatorEngine {
private static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign"; private static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign";
private static final String MULTIPLICATION_SIGN_DEFAULT = "×"; private static final String MULTIPLICATION_SIGN_DEFAULT = "×";
private static final String SCIENCE_NOTATION_P_KEY = "calculation.output.science_notation";
private static final boolean SCIENCE_NOTATION_DEFAULT = false;
private static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result"; private static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result";
private static final boolean ROUND_RESULT_DEFAULT = true; private static final boolean ROUND_RESULT_DEFAULT = true;
@ -60,14 +63,16 @@ public enum CalculatorEngine {
private static final String ANGLE_UNITS_DEFAULT = "deg"; private static final String ANGLE_UNITS_DEFAULT = "deg";
public static class Preferences { public static class Preferences {
public static final Preference<String> groupingSeparator = StringPreference.newInstance(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); public static final Preference<String> groupingSeparator = StringPreference.newInstance(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
public static final Preference<String> multiplicationSign = StringPreference.newInstance(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT); public static final Preference<String> multiplicationSign = StringPreference.newInstance(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT);
public static final Preference<Integer> precision = StringPreference.newInstance(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, new NumberMapper<Integer>(Integer.class)); public static final Preference<Integer> precision = StringPreference.newInstance(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, new NumberMapper<Integer>(Integer.class));
public static final Preference<Boolean> roundResult = new BooleanPreference(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT); public static final Preference<Boolean> roundResult = new BooleanPreference(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT);
public static final Preference<NumeralBase> numeralBase = StringPreference.newInstance(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.newInstance(NumeralBase.class)); public static final Preference<NumeralBase> numeralBase = StringPreference.newInstance(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.newInstance(NumeralBase.class));
public static final Preference<AngleUnit> angleUnit = StringPreference.newInstance(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.newInstance(AngleUnit.class)); public static final Preference<AngleUnit> angleUnit = StringPreference.newInstance(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.newInstance(AngleUnit.class));
public static final Preference<Boolean> scienceNotation = new BooleanPreference(SCIENCE_NOTATION_P_KEY, SCIENCE_NOTATION_DEFAULT);
private static final List<String> preferenceKeys = new ArrayList<String>(); private static final List<String> preferenceKeys = new ArrayList<String>();
static { static {
preferenceKeys.add(groupingSeparator.getKey()); preferenceKeys.add(groupingSeparator.getKey());
preferenceKeys.add(multiplicationSign.getKey()); preferenceKeys.add(multiplicationSign.getKey());
@ -75,11 +80,12 @@ public enum CalculatorEngine {
preferenceKeys.add(roundResult.getKey()); preferenceKeys.add(roundResult.getKey());
preferenceKeys.add(numeralBase.getKey()); preferenceKeys.add(numeralBase.getKey());
preferenceKeys.add(angleUnit.getKey()); preferenceKeys.add(angleUnit.getKey());
preferenceKeys.add(scienceNotation.getKey());
} }
@NotNull @NotNull
public static List<String> getPreferenceKeys() { public static List<String> getPreferenceKeys() {
return Collections.unmodifiableList(preferenceKeys); return Collections.unmodifiableList(preferenceKeys);
} }
} }
@ -255,7 +261,7 @@ public enum CalculatorEngine {
if (parseExceptionObject != null || evalExceptionObject != null) { if (parseExceptionObject != null || evalExceptionObject != null) {
if (operation == JsclOperation.numeric && if (operation == JsclOperation.numeric &&
( preparedExpression.isExistsUndefinedVar() || ( evalExceptionObject != null && evalExceptionObject.getCause() instanceof NumeralBaseException)) ) { (preparedExpression.isExistsUndefinedVar() || (evalExceptionObject != null && evalExceptionObject.getCause() instanceof NumeralBaseException))) {
return evaluate(JsclOperation.simplify, expression, mr); return evaluate(JsclOperation.simplify, expression, mr);
} }
@ -296,21 +302,7 @@ public enum CalculatorEngine {
public void reset(@Nullable Context context, @Nullable SharedPreferences preferences) { public void reset(@Nullable Context context, @Nullable SharedPreferences preferences) {
synchronized (lock) { synchronized (lock) {
if (preferences != null) { softReset(context, preferences);
this.setPrecision(Preferences.precision.getPreference(preferences));
this.setRoundResult(Preferences.roundResult.getPreference(preferences));
this.setAngleUnits(getAngleUnitsFromPrefs(preferences));
this.setNumeralBase(getNumeralBaseFromPrefs(preferences));
this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences));
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences);
if (StringUtils.isEmpty(groupingSeparator)) {
this.getEngine().setUseGroupingSeparator(false);
} else {
this.getEngine().setUseGroupingSeparator(true);
this.getEngine().setGroupingSeparator(groupingSeparator.charAt(0));
}
}
varsRegistry.load(context, preferences); varsRegistry.load(context, preferences);
functionsRegistry.load(context, preferences); functionsRegistry.load(context, preferences);
@ -327,6 +319,7 @@ public enum CalculatorEngine {
this.setAngleUnits(getAngleUnitsFromPrefs(preferences)); this.setAngleUnits(getAngleUnitsFromPrefs(preferences));
this.setNumeralBase(getNumeralBaseFromPrefs(preferences)); this.setNumeralBase(getNumeralBaseFromPrefs(preferences));
this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences)); this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences));
this.setScienceNotation(Preferences.scienceNotation.getPreference(preferences));
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences);
if (StringUtils.isEmpty(groupingSeparator)) { if (StringUtils.isEmpty(groupingSeparator)) {
@ -391,6 +384,10 @@ public enum CalculatorEngine {
getEngine().setAngleUnits(angleUnits); getEngine().setAngleUnits(angleUnits);
} }
public void setScienceNotation(boolean scienceNotation) {
getEngine().setScienceNotation(scienceNotation);
}
public void setNumeralBase(@NotNull NumeralBase numeralBase) { public void setNumeralBase(@NotNull NumeralBase numeralBase) {
getEngine().setNumeralBase(numeralBase); getEngine().setNumeralBase(numeralBase);
} }

View File

@ -261,6 +261,14 @@ public class CalculatorEngineTest {
Assert.assertEquals("∫((ln(2)+ln(5))/ln(x), x)", cm.getEngine().simplify("∫(log(x, 10), x)")); Assert.assertEquals("∫((ln(2)+ln(5))/ln(x), x)", cm.getEngine().simplify("∫(log(x, 10), x)"));
} }
@Test
public void testFormatting() throws Exception {
final CalculatorEngine ce = CalculatorEngine.instance;
Assert.assertEquals("12 345", ce.evaluate(JsclOperation.simplify, "12345").getResult());
}
@Test @Test
public void testI() throws CalculatorParseException, CalculatorEvalException { public void testI() throws CalculatorParseException, CalculatorEvalException {
final CalculatorEngine cm = CalculatorEngine.instance; final CalculatorEngine cm = CalculatorEngine.instance;