From c521a5577a42ed118909f20cc265aa35daf25e97 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 28 Oct 2011 01:29:48 +0400 Subject: [PATCH] sqrt + log10 + ln --- .../android/calculator/math/Functions.java | 5 ++- .../android/calculator/math/MathType.java | 34 ++----------------- .../calculator/model/CalculatorEngine.java | 2 +- .../model/CalculatorEngineTest.java | 10 +++--- .../FromJsclSimplifyTextProcessorTest.java | 4 +-- .../model/ToJsclTextProcessorTest.java | 18 +++++----- 6 files changed, 21 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/solovyev/android/calculator/math/Functions.java b/src/main/java/org/solovyev/android/calculator/math/Functions.java index db1890d0..e847414f 100644 --- a/src/main/java/org/solovyev/android/calculator/math/Functions.java +++ b/src/main/java/org/solovyev/android/calculator/math/Functions.java @@ -33,18 +33,17 @@ public class Functions { public final static String ATANH = "atanh"; public final static String LN = "ln"; - public final static String LN_JSCL = "log"; + public final static String LOG = "log"; public final static String MOD = "mod"; public final static String EXP = "exp"; public final static String SQRT = "√"; - public final static String SQRT_JSCL = "sqrt"; public static final List allPrefix; static { - final List functions = new ArrayList(Arrays.asList(SIN, SINH, ASIN, ASINH, COS, COSH, ACOS, ACOSH, TAN, TANH, ATAN, ATANH, LN, LN_JSCL, MOD, SQRT, SQRT_JSCL, EXP)); + final List functions = new ArrayList(Arrays.asList(SIN, SINH, ASIN, ASINH, COS, COSH, ACOS, ACOSH, TAN, TANH, ATAN, ATANH, LN, LOG, MOD, SQRT, EXP)); Collections.sort(functions, new MathEntityComparator()); allPrefix = functions; } diff --git a/src/main/java/org/solovyev/android/calculator/math/MathType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java index 23d1a8e2..9d4cdec6 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -129,37 +129,7 @@ public enum MathType { } }, - function(1000, true, true, Functions.allPrefix) { - @Override - protected String getSubstituteToJscl(@NotNull String match) { - final String result; - - if (match.equals(Functions.LN)) { - result = Functions.LN_JSCL; - } else if (match.equals(Functions.SQRT)) { - result = Functions.SQRT_JSCL; - } else { - result = null; - } - - return result; - } - - @Override - protected String getSubstituteFromJscl(@NotNull String match) { - final String result; - - if (match.equals(Functions.LN_JSCL)) { - result = Functions.LN; - } else if (match.equals(Functions.SQRT_JSCL)) { - result = Functions.SQRT; - } else { - result = null; - } - - return result; - } - }, + function(1000, true, true, Functions.allPrefix), constant(1100, true, true) { @NotNull @@ -317,7 +287,7 @@ public enum MathType { public final static String POWER_10_JSCL = "10^"; public static final String IMAGINARY_NUMBER = "i"; - public static final String IMAGINARY_NUMBER_JSCL = "sqrt(-1)"; + public static final String IMAGINARY_NUMBER_JSCL = "√(-1)"; public static final String PI = "π"; public static final String E = "e"; 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 5f5034fe..b4d334fa 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -79,7 +79,7 @@ public enum CalculatorEngine { private ThreadKiller threadKiller = new AndroidThreadKiller(); // calculation thread timeout in milliseconds, after timeout thread would be interrupted - private int timeout = 2000; + private int timeout = 3000; @NotNull public String format(@NotNull Double value) { 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 2401112e..c2991963 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -67,12 +67,12 @@ public class CalculatorEngineTest { 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)sqrt(2)").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()); 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+sqrt(-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()); Assert.assertEquals("7.389i", cm.evaluate(JsclOperation.numeric, "iexp(2)").getResult()); Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+iexp(2)").getResult()); @@ -118,12 +118,12 @@ public class CalculatorEngineTest { } } - try { +/* try { cm.setTimeout(5000); Assert.assertEquals("2", cm.evaluate(JsclOperation.numeric, "2!").getResult()); } finally { - cm.setTimeout(2000); - } + cm.setTimeout(3000); + }*/ } diff --git a/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java b/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java index 3462e03d..b0579ba9 100644 --- a/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java @@ -58,8 +58,8 @@ public class FromJsclSimplifyTextProcessorTest { Assert.assertEquals("(te+2k)e[k+2te]", tp.process("(t*e+2*k)*e*[k+2*t*e]")); - Assert.assertEquals("tln(3)", tp.process("t*log(3)")); - Assert.assertEquals("t√(3)", tp.process("t*sqrt(3)")); + Assert.assertEquals("tlog(3)", tp.process("t*log(3)")); + Assert.assertEquals("t√(3)", tp.process("t*√(3)")); } } diff --git a/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index a3cba032..fc2eb4a0 100644 --- a/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -42,16 +42,16 @@ public class ToJsclTextProcessorTest { Assert.assertEquals( "(0)*(1*10^-1)", preprocessor.process("[0][1E-1]").toString()); Assert.assertEquals( "(0)*(1.*10^-1)", preprocessor.process("[0][1.E-1]").toString()); Assert.assertEquals( "(0)*(2*10^-1)", preprocessor.process("[0][2*E-1]").toString()); - Assert.assertEquals( "(0)*log(1)*(2*10^-1)", preprocessor.process("[0]ln(1)[2*E-1]").toString()); - Assert.assertEquals( "sin(4)*asin(0.5)*sqrt(2)", preprocessor.process("sin(4)asin(0.5)sqrt(2)").toString()); + Assert.assertEquals( "(0)*ln(1)*(2*10^-1)", preprocessor.process("[0]ln(1)[2*E-1]").toString()); + Assert.assertEquals( "sin(4)*asin(0.5)*√(2)", preprocessor.process("sin(4)asin(0.5)√(2)").toString()); Assert.assertEquals( "sin(4)*cos(5)", preprocessor.process("sin(4)cos(5)").toString()); - Assert.assertEquals( "3.141592653589793*sin(4)*3.141592653589793*cos(sqrt(5))", preprocessor.process("πsin(4)πcos(√(5))").toString()); - Assert.assertEquals( "3.141592653589793*sin(4)+3.141592653589793*cos(sqrt(5))", preprocessor.process("πsin(4)+πcos(√(5))").toString()); - Assert.assertEquals( "3.141592653589793*sin(4)+3.141592653589793*cos(sqrt(5+sqrt(-1)))", preprocessor.process("πsin(4)+πcos(√(5+i))").toString()); - Assert.assertEquals( "3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))", preprocessor.process("πsin(4.01)+πcos(√(5+i))").toString()); - Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))").toString()); - Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))*10^2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E2").toString()); - Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))*10^-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2").toString()); + Assert.assertEquals( "3.141592653589793*sin(4)*3.141592653589793*cos(√(5))", preprocessor.process("πsin(4)πcos(√(5))").toString()); + Assert.assertEquals( "3.141592653589793*sin(4)+3.141592653589793*cos(√(5))", preprocessor.process("πsin(4)+πcos(√(5))").toString()); + Assert.assertEquals( "3.141592653589793*sin(4)+3.141592653589793*cos(√(5+√(-1)))", preprocessor.process("πsin(4)+πcos(√(5+i))").toString()); + Assert.assertEquals( "3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+√(-1)))", preprocessor.process("πsin(4.01)+πcos(√(5+i))").toString()); + Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+√(-1)))", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))").toString()); + Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+√(-1)))*10^2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E2").toString()); + Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+√(-1)))*10^-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2").toString()); Assert.assertEquals( "10^2", preprocessor.process("E2").toString()); Assert.assertEquals( "10^-2", preprocessor.process("E-2").toString()); Assert.assertEquals( "10^-1/2", preprocessor.process("E-1/2").toString());