symbolic calculations added

This commit is contained in:
serso 2011-10-24 23:11:50 +04:00
parent a6caf0be93
commit 5abb206529
16 changed files with 270 additions and 123 deletions

View File

@ -25,9 +25,9 @@
calc:directionTextScale="0.5"
style="?controlButtonStyle"
a:onClick="moveLeftButtonClickHandler"
a:layout_width="fill_parent"
a:layout_width="0dp"
a:layout_height="fill_parent"
a:layout_weight="1.5"/>
a:layout_weight="1"/>
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
@ -36,15 +36,15 @@
a:drawableTop="@drawable/sym_keyboard_delete"
style="?controlImageButtonStyle"
a:onClick="eraseButtonClickHandler"
a:layout_width="fill_parent"
a:layout_width="0dp"
a:layout_height="fill_parent"
a:layout_weight="1.5"/>
a:layout_weight="1"/>
<include layout="@layout/calc_display"
a:layout_width="fill_parent"
a:layout_width="0dp"
a:layout_height="fill_parent"
a:layout_weight="1"/>
a:layout_weight="4"/>
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
@ -53,9 +53,9 @@
a:textStyle="bold"
style="?controlImageButtonStyle"
a:onClick="clearButtonClickHandler"
a:layout_width="fill_parent"
a:layout_width="0dp"
a:layout_height="fill_parent"
a:layout_weight="1.5"/>
a:layout_weight="1"/>
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
@ -65,9 +65,9 @@
calc:directionTextScale="0.5"
style="?controlButtonStyle"
a:onClick="moveRightButtonClickHandler"
a:layout_width="fill_parent"
a:layout_width="0dp"
a:layout_height="fill_parent"
a:layout_weight="1.5"/>
a:layout_weight="1"/>
</LinearLayout>
@ -80,6 +80,7 @@
<include layout="@layout/calc_three_digit_button"/>
<include layout="@layout/calc_multiplication_button"/>
<include layout="@layout/calc_division_button"/>
<include layout="@layout/calc_equals_button"/>
</LinearLayout>
@ -92,19 +93,20 @@
<include layout="@layout/calc_six_digit_button"/>
<include layout="@layout/calc_plus_button"/>
<include layout="@layout/calc_subtraction_button"/>
<include layout="@layout/calc_empty_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_donate_button"/>
<include layout="@layout/calc_square_brackets_button"/>
<include layout="@layout/calc_empty_button"/>
<include layout="@layout/calc_seven_digit_button"/>
<include layout="@layout/calc_eight_digit_button"/>
<include layout="@layout/calc_nine_digit_button"/>
<include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_round_brackets_button"/>
<include layout="@layout/calc_square_brackets_button"/>
</LinearLayout>

View File

@ -16,7 +16,13 @@
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_equals_button"
a:layout_weight="4"
a:layout_width="fill_parent"
a:layout_height="fill_parent"/>
<include layout="@layout/calc_display"
a:layout_weight="1"
a:layout_width="fill_parent"
a:layout_height="fill_parent"/>

View File

@ -10,7 +10,6 @@
xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/calculatorDisplay"
style="@style/display_style"
a:layout_weight="1"
a:minLines="1"
a:maxLines="1"
a:textStyle="bold"

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org
-->
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
style="?controlButtonStyle"/>

View File

@ -7,8 +7,11 @@
-->
<org.solovyev.android.view.widgets.DirectionDragButton
xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/equalsButton"
calc:textUp="≡"
a:text="="
calc:directionTextScale="0.5"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
style="?controlButtonStyle"

View File

@ -92,6 +92,13 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
dpclRegister.addListener(toPositionOnDragListener);
final DragButton equalsButton = (DragButton) findViewById(R.id.equalsButton);
if (equalsButton != null) {
final SimpleOnDragListener evalOnDragListener = new SimpleOnDragListener(new EvalDragProcessor(calculatorModel), dragPreferences);
equalsButton.setOnDragListener(evalOnDragListener);
dpclRegister.addListener(evalOnDragListener);
}
CalculatorEngine.instance.reset(this, preferences);
preferences.registerOnSharedPreferenceChangeListener(this);
@ -389,10 +396,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
}
calculatorModel = CalculatorModel.instance.init(this, preferences, CalculatorEngine.instance);
AndroidMessageRegistry.instance.init(this);
this.calculatorModel.evaluate();
}
@Override

View File

@ -12,6 +12,7 @@ import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.android.calculator.model.TextProcessor;
@ -24,6 +25,9 @@ public class CalculatorDisplay extends TextView {
private boolean valid = true;
@NotNull
private JsclOperation jsclOperation = JsclOperation.numeric;
@NotNull
private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE, true);
@ -47,6 +51,15 @@ public class CalculatorDisplay extends TextView {
this.valid = valid;
}
public void setJsclOperation(@NotNull JsclOperation jsclOperation) {
this.jsclOperation = jsclOperation;
}
@NotNull
public JsclOperation getJsclOperation() {
return jsclOperation;
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);

View File

@ -6,7 +6,7 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.jscl.JsclOperation;
/**
* User: serso
@ -18,45 +18,49 @@ public class CalculatorDisplayHistoryState {
private boolean valid = true;
@NotNull
private final EditorHistoryState editorHistoryState;
private EditorHistoryState editorHistoryState;
public CalculatorDisplayHistoryState() {
this.editorHistoryState = new EditorHistoryState();
@NotNull
private JsclOperation jsclOperation;
private CalculatorDisplayHistoryState() {
}
public CalculatorDisplayHistoryState(boolean valid) {
this.editorHistoryState = new EditorHistoryState();
this.valid = valid;
}
@NotNull
public static CalculatorDisplayHistoryState newInstance(@NotNull CalculatorDisplay display) {
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState();
public CalculatorDisplayHistoryState(int cursorPosition, @Nullable String text, boolean valid) {
this.editorHistoryState = new EditorHistoryState(cursorPosition, text);
this.valid = valid;
result.editorHistoryState = EditorHistoryState.newInstance(display);
result.valid = display.isValid();
result.jsclOperation = display.getJsclOperation();
return result;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
@NotNull
public EditorHistoryState getEditorHistoryState() {
return editorHistoryState;
}
@NotNull
public JsclOperation getJsclOperation() {
return jsclOperation;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CalculatorDisplayHistoryState)) return false;
if (o == null || getClass() != o.getClass()) return false;
CalculatorDisplayHistoryState that = (CalculatorDisplayHistoryState) o;
if (valid != that.valid) return false;
if (editorHistoryState != null ? !editorHistoryState.equals(that.editorHistoryState) : that.editorHistoryState != null)
return false;
if (!editorHistoryState.equals(that.editorHistoryState)) return false;
if (jsclOperation != that.jsclOperation) return false;
return true;
}
@ -64,7 +68,8 @@ public class CalculatorDisplayHistoryState {
@Override
public int hashCode() {
int result = (valid ? 1 : 0);
result = 31 * result + (editorHistoryState != null ? editorHistoryState.hashCode() : 0);
result = 31 * result + editorHistoryState.hashCode();
result = 31 * result + jsclOperation.hashCode();
return result;
}
@ -72,6 +77,7 @@ public class CalculatorDisplayHistoryState {
public String toString() {
return "CalculatorDisplayHistoryState{" +
"valid=" + valid +
"jsclOperation=" + jsclOperation +
", editorHistoryState=" + editorHistoryState +
'}';
}

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
/**
* User: serso
* Date: 10/24/11
* Time: 9:55 PM
*/
public interface CalculatorEngineControl {
void evaluate();
void simplify();
}

View File

@ -12,6 +12,7 @@ import android.os.Bundle;
import android.view.*;
import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.utils.Filter;
import org.solovyev.common.utils.FilterRule;
import org.solovyev.common.utils.FilterRulesChain;
@ -53,14 +54,16 @@ public class CalculatorHistoryActivity extends ListActivity {
final int position,
final long id) {
final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position);
CalculatorModel.instance.doTextOperation(new CalculatorModel.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
final EditorHistoryState editorState = ((CalculatorHistoryState) parent.getItemAtPosition(position)).getEditorState();
final EditorHistoryState editorState = historyState.getEditorState();
editor.setText(editorState.getText());
editor.setSelection(editorState.getCursorPosition());
}
}, false);
}, false, historyState.getDisplayState().getJsclOperation(), true);
CalculatorModel.instance.setCursorOnEnd();
@ -108,10 +111,15 @@ public class CalculatorHistoryActivity extends ListActivity {
time.setText(new SimpleDateFormat().format(state.getTime()));
final TextView editor = (TextView) result.findViewById(R.id.history_item);
editor.setText(state.getEditorState().getText() + "=" + state.getDisplayState().getEditorHistoryState().getText());
editor.setText(state.getEditorState().getText() + getIdentitySign(state.getDisplayState().getJsclOperation()) + state.getDisplayState().getEditorHistoryState().getText());
return result;
}
@NotNull
private String getIdentitySign(@NotNull JsclOperation jsclOperation) {
return jsclOperation == JsclOperation.simplify ? "" : "=";
}
}
@Override

View File

@ -34,7 +34,7 @@ import org.solovyev.common.utils.history.HistoryAction;
* Date: 9/12/11
* Time: 11:15 PM
*/
public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorHistoryState> {
public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorHistoryState>, CalculatorEngineControl {
instance;
@ -51,6 +51,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
private CalculatorEngine calculatorEngine;
public CalculatorModel init(@NotNull final Activity activity, @NotNull SharedPreferences preferences, @NotNull CalculatorEngine calculator) {
Log.d(this.getClass().getName(), "CalculatorModel initialization with activity: " + activity);
this.calculatorEngine = calculator;
this.editor = (CalculatorEditor) activity.findViewById(R.id.calculatorEditor);
@ -119,24 +120,29 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
}
public void doTextOperation(@NotNull TextOperation operation, boolean delayEvaluate) {
doTextOperation(operation, delayEvaluate, JsclOperation.numeric, false);
}
public void doTextOperation(@NotNull TextOperation operation, boolean delayEvaluate, @NotNull JsclOperation jsclOperation, boolean forceEval) {
final String editorStateBefore = this.editor.getText().toString();
Log.d(CalculatorModel.class.getName(), "Editor state changed before '" + editorStateBefore + "'");
operation.doOperation(this.editor);
//Log.d(CalculatorModel.class.getName(), "Doing text operation" + StringUtils.fromStackTrace(Thread.currentThread().getStackTrace()));
final String editorStateAfter = this.editor.getText().toString();
if (!editorStateBefore.equals(editorStateAfter)) {
if (forceEval ||!editorStateBefore.equals(editorStateAfter)) {
editor.redraw();
evaluate(delayEvaluate, editorStateAfter);
evaluate(delayEvaluate, editorStateAfter, jsclOperation);
}
}
@NotNull
private final static MutableObject<Runnable> pendingOperation = new MutableObject<Runnable>();
private void evaluate(boolean delayEvaluate, @NotNull final String expression) {
private void evaluate(boolean delayEvaluate, @NotNull final String expression, @NotNull final JsclOperation operation) {
final CalculatorHistoryState historyState = getCurrentHistoryState();
pendingOperation.setObject(new Runnable() {
@ -147,7 +153,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
//lock all operations with history
if (pendingOperation.getObject() == this) {
// actually nothing shall be logged while text operations are done
evaluate(expression);
evaluate(expression, operation);
historyState.setDisplayState(getCurrentHistoryState().getDisplayState());
@ -166,19 +172,27 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
}
}
@Override
public void evaluate() {
evaluate(false, this.editor.getText().toString());
evaluate(false, this.editor.getText().toString(), JsclOperation.numeric);
}
private void evaluate(@Nullable final String expression) {
@Override
public void simplify() {
evaluate(false, this.editor.getText().toString(), JsclOperation.simplify);
}
private void evaluate(@Nullable final String expression, @NotNull JsclOperation operation) {
if (!StringUtils.isEmpty(expression)) {
try {
Log.d(CalculatorModel.class.getName(), "Trying to evaluate: " + expression /*+ StringUtils.fromStackTrace(Thread.currentThread().getStackTrace())*/);
display.setText(calculatorEngine.evaluate(JsclOperation.numeric, expression));
Log.d(CalculatorModel.class.getName(), "Trying to evaluate '" + operation + "': " + expression /*+ StringUtils.fromStackTrace(Thread.currentThread().getStackTrace())*/);
final CalculatorEngine.Result result = calculatorEngine.evaluate(operation, expression);
display.setText(result.getResult());
display.setJsclOperation(result.getUserOperation());
} catch (EvalError e) {
handleEvaluationException(expression, display, e);
handleEvaluationException(expression, display, operation, e);
} catch (ParseException e) {
handleEvaluationException(expression, display, e);
handleEvaluationException(expression, display, operation, e);
}
} else {
this.display.setText("");
@ -187,9 +201,13 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
this.display.redraw();
}
private void handleEvaluationException(@NotNull String expression, @NotNull CalculatorDisplay localDisplay, @NotNull Exception e) {
private void handleEvaluationException(@NotNull String expression,
@NotNull CalculatorDisplay localDisplay,
@NotNull JsclOperation operation,
@NotNull Exception e) {
Log.d(CalculatorModel.class.getName(), "Evaluation failed for : " + expression + ". Error message: " + e.getMessage());
localDisplay.setText(R.string.c_syntax_error);
localDisplay.setJsclOperation(operation);
localDisplay.setValid(false);
}
@ -268,6 +286,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
private void setValuesFromHistory(@NotNull CalculatorDisplay display, CalculatorDisplayHistoryState editorHistoryState) {
setValuesFromHistory(display, editorHistoryState.getEditorHistoryState());
display.setValid(editorHistoryState.isValid());
display.setJsclOperation(editorHistoryState.getJsclOperation());
}
private void setValuesFromHistory(@NotNull TextView editText, EditorHistoryState editorHistoryState) {
@ -286,22 +305,11 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
}
private EditorHistoryState getEditorHistoryState(@NotNull TextView textView) {
final EditorHistoryState result = new EditorHistoryState();
result.setText(String.valueOf(textView.getText()));
result.setCursorPosition(textView.getSelectionStart());
return result;
return EditorHistoryState.newInstance(textView);
}
private CalculatorDisplayHistoryState getCalculatorDisplayHistoryState(@NotNull CalculatorDisplay display) {
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState();
result.getEditorHistoryState().setText(String.valueOf(display.getText()));
result.getEditorHistoryState().setCursorPosition(display.getSelectionStart());
result.setValid(display.isValid());
return result;
return CalculatorDisplayHistoryState.newInstance(display);
}
@NotNull

View File

@ -36,8 +36,10 @@ public class CursorDragProcessor implements SimpleOnDragListener.DragProcessor{
String text = ((DirectionDragButton) dragButton).getText(dragDirection);
if ("◀◀".equals(text)) {
cursorControl.setCursorOnStart();
result = true;
} else if ("▶▶".equals(text)) {
cursorControl.setCursorOnEnd();
result = true;
}
}

View File

@ -5,25 +5,29 @@
package org.solovyev.android.calculator;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class EditorHistoryState {
private int cursorPosition;
@Nullable
private String text;
public EditorHistoryState() {
}
public EditorHistoryState( int cursorPosition, @Nullable String text ) {
this.cursorPosition = cursorPosition;
this.text = text;
private EditorHistoryState() {
}
public void setText(@Nullable String text) {
this.text = text;
@NotNull
public static EditorHistoryState newInstance(@NotNull TextView textView) {
final EditorHistoryState result = new EditorHistoryState();
result.text = String.valueOf(textView.getText());
result.cursorPosition = textView.getSelectionStart();
return result;
}
@Nullable
@ -31,10 +35,6 @@ public class EditorHistoryState {
return text;
}
public void setCursorPosition(int cursorPosition) {
this.cursorPosition = cursorPosition;
}
public int getCursorPosition() {
return cursorPosition;
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
import android.view.MotionEvent;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.view.widgets.DirectionDragButton;
import org.solovyev.android.view.widgets.DragButton;
import org.solovyev.android.view.widgets.DragDirection;
import org.solovyev.android.view.widgets.SimpleOnDragListener;
import org.solovyev.common.utils.Point2d;
/**
* User: serso
* Date: 10/24/11
* Time: 9:52 PM
*/
public class EvalDragProcessor implements SimpleOnDragListener.DragProcessor {
@NotNull
private final CalculatorEngineControl calculatorControl;
public EvalDragProcessor(@NotNull CalculatorEngineControl calculatorControl) {
this.calculatorControl = calculatorControl;
}
@Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
boolean result = false;
if (dragButton instanceof DirectionDragButton) {
String text = ((DirectionDragButton) dragButton).getText(dragDirection);
if ("".equals(text)) {
calculatorControl.simplify();
result = true;
}
}
return result;
}
}

View File

@ -98,12 +98,35 @@ public enum CalculatorEngine {
}
}
public String evaluate(@NotNull JsclOperation operation,
public static class Result {
@NotNull
private String result;
@NotNull
private JsclOperation userOperation;
public Result(@NotNull String result, @NotNull JsclOperation userOperation) {
this.result = result;
this.userOperation = userOperation;
}
@NotNull
public String getResult() {
return result;
}
@NotNull
public JsclOperation getUserOperation() {
return userOperation;
}
}
public Result evaluate(@NotNull JsclOperation operation,
@NotNull String expression) throws EvalError, ParseException {
return evaluate(operation, expression, null);
}
public String evaluate(@NotNull JsclOperation operation,
public Result evaluate(@NotNull JsclOperation operation,
@NotNull String expression,
@Nullable MessageRegistry<AndroidMessage> mr) throws EvalError, ParseException {
synchronized (lock) {
@ -190,7 +213,7 @@ public enum CalculatorEngine {
throw new ParseException("Too long calculation for: " + jsclExpression);
}
return operation.getFromProcessor().process(result);
return new Result(operation.getFromProcessor().process(result), operation);
}
}

View File

@ -66,54 +66,54 @@ public class CalculatorEngineTest {
public void testEvaluate() throws Exception {
final CalculatorEngine cm = CalculatorEngine.instance;
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2"));
Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)"));
Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)"));
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)sqrt(2)"));
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)"));
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "e^2"));
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(1)^2"));
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(2)"));
Assert.assertEquals("2+i", cm.evaluate(JsclOperation.numeric, "2*1+sqrt(-1)"));
Assert.assertEquals("0.921+3.142i", cm.evaluate(JsclOperation.numeric, "ln(5cosh(38π√(2cos(2))))"));
Assert.assertEquals("7.389i", cm.evaluate(JsclOperation.numeric, "iexp(2)"));
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+iexp(2)"));
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(2)"));
Assert.assertEquals("2-2.5i", cm.evaluate(JsclOperation.numeric, "2-2.5i"));
Assert.assertEquals("-2-2.5i", cm.evaluate(JsclOperation.numeric, "-2-2.5i"));
Assert.assertEquals("-2+2.5i", cm.evaluate(JsclOperation.numeric, "-2+2.5i"));
Assert.assertEquals("-2+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i"));
Assert.assertEquals("-3.41+3.41i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)"));
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)"));
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)sqrt(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("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());
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(2)").getResult());
Assert.assertEquals("2-2.5i", cm.evaluate(JsclOperation.numeric, "2-2.5i").getResult());
Assert.assertEquals("-2-2.5i", cm.evaluate(JsclOperation.numeric, "-2-2.5i").getResult());
Assert.assertEquals("-2+2.5i", cm.evaluate(JsclOperation.numeric, "-2+2.5i").getResult());
Assert.assertEquals("-2+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i").getResult());
Assert.assertEquals("-3.41+3.41i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)").getResult());
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)").getResult());
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
Assert.assertEquals("-0.959", cm.evaluate(JsclOperation.numeric, "sin(5)"));
Assert.assertEquals("-4.795", cm.evaluate(JsclOperation.numeric, "sin(5)si"));
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "sisin(5)si"));
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "si*sin(5)si"));
Assert.assertEquals("-3.309", cm.evaluate(JsclOperation.numeric, "sisin(5si)si"));
Assert.assertEquals("-0.959", cm.evaluate(JsclOperation.numeric, "sin(5)").getResult());
Assert.assertEquals("-4.795", cm.evaluate(JsclOperation.numeric, "sin(5)si").getResult());
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "sisin(5)si").getResult());
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "si*sin(5)si").getResult());
Assert.assertEquals("-3.309", cm.evaluate(JsclOperation.numeric, "sisin(5si)si").getResult());
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("s", 1d));
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si"));
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11"));
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11").getResult());
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t", (String) null));
Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11"));
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e"));
Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞"));
Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)"));
Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getResult());
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getResult());
Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞").getResult());
Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)").getResult());
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "30°"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "(10+20)°"));
Assert.assertEquals("1.047", cm.evaluate(JsclOperation.numeric, "(10+20)°*2"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "30°").getResult());
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "(10+20)°").getResult());
Assert.assertEquals("1.047", cm.evaluate(JsclOperation.numeric, "(10+20)°*2").getResult());
try {
Assert.assertEquals("0.278", cm.evaluate(JsclOperation.numeric, "30°^2"));
Assert.assertEquals("0.278", cm.evaluate(JsclOperation.numeric, "30°^2").getResult());
junit.framework.Assert.fail();
} catch (ParseException e) {
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
@ -130,16 +130,16 @@ public class CalculatorEngineTest {
Assert.fail();
} catch (ParseException e) {
}
Assert.assertEquals("NaN", cm.evaluate(JsclOperation.numeric, "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))"));
Assert.assertEquals("NaN", cm.evaluate(JsclOperation.numeric, "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))").getResult());
try {
cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos())))))))))))))))))))))))))))))))))))");
Assert.fail();
} catch (ParseException e) {
}
Assert.assertEquals("0.739", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(1))))))))))))))))))))))))))))))))))))"));
Assert.assertEquals("0.739", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(1))))))))))))))))))))))))))))))))))))").getResult());
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si"));
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
try {
cm.evaluate(JsclOperation.numeric, "sin");
Assert.fail();
@ -156,11 +156,11 @@ public class CalculatorEngineTest {
decimalGroupSymbols.setGroupingSeparator('\'');
cm.setDecimalGroupSymbols(decimalGroupSymbols);
cm.setPrecision(2);
Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7"));
Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7").getResult());
cm.setPrecision(10);
Assert.assertEquals("12'345'678.899999999", cm.evaluate(JsclOperation.numeric, "1.23456789E7"));
Assert.assertEquals("123'456'788.99999999", cm.evaluate(JsclOperation.numeric, "1.234567890E8"));
Assert.assertEquals("1'234'567'890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9"));
Assert.assertEquals("12'345'678.899999999", cm.evaluate(JsclOperation.numeric, "1.23456789E7").getResult());
Assert.assertEquals("123'456'788.99999999", cm.evaluate(JsclOperation.numeric, "1.234567890E8").getResult());
Assert.assertEquals("1'234'567'890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9").getResult());
}