diff --git a/apps/calculation/calculation.cpp b/apps/calculation/calculation.cpp index 349f2b966..b8d9c7e41 100644 --- a/apps/calculation/calculation.cpp +++ b/apps/calculation/calculation.cpp @@ -59,7 +59,7 @@ void Calculation::setContent(const char * c, Context * context, Preferences * pr if (m_output != nullptr) { delete m_output; } - m_output = m_input->evaluate(*context); + m_output = m_input->evaluate(*context, preferences->angleUnit()); if (m_outputLayout != nullptr) { delete m_outputLayout; } diff --git a/apps/editable_cell_table_view_controller.cpp b/apps/editable_cell_table_view_controller.cpp index 23f7fc6ed..8988a27c7 100644 --- a/apps/editable_cell_table_view_controller.cpp +++ b/apps/editable_cell_table_view_controller.cpp @@ -23,7 +23,7 @@ bool EditableCellTableViewController::textFieldDidReceiveEvent(TextField * textF bool EditableCellTableViewController::textFieldDidFinishEditing(TextField * textField, const char * text) { AppsContainer * appsContainer = (AppsContainer *)app()->container(); Context * globalContext = appsContainer->globalContext(); - float floatBody = Expression::parse(text)->approximate(*globalContext); + float floatBody = Expression::parse(text)->approximate(*globalContext, appsContainer->preferences()->angleUnit()); setDataAtLocation(floatBody, m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()); willDisplayCellAtLocation(m_selectableTableView.cellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()), m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()); m_selectableTableView.reloadData(); diff --git a/apps/float_parameter_controller.cpp b/apps/float_parameter_controller.cpp index f0fb42765..81588e809 100644 --- a/apps/float_parameter_controller.cpp +++ b/apps/float_parameter_controller.cpp @@ -34,7 +34,7 @@ void FloatParameterController::willDisplayCellForIndex(TableViewCell * cell, int bool FloatParameterController::textFieldDidFinishEditing(TextField * textField, const char * text) { AppsContainer * appsContainer = (AppsContainer *)app()->container(); Context * globalContext = appsContainer->globalContext(); - float floatBody = Expression::parse(text)->approximate(*globalContext); + float floatBody = Expression::parse(text)->approximate(*globalContext, appsContainer->preferences()->angleUnit()); setParameterAtIndex(m_selectableTableView.selectedRow(), floatBody); willDisplayCellForIndex(m_selectableTableView.cellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()), activeCell()); diff --git a/apps/graph/function.cpp b/apps/graph/function.cpp index b25f5def9..4df3c95bc 100644 --- a/apps/graph/function.cpp +++ b/apps/graph/function.cpp @@ -75,19 +75,19 @@ void Function::setDisplayDerivative(bool display) { m_displayDerivative = display; } -float Function::evaluateAtAbscissa(float x, Context * context) const { +float Function::evaluateAtAbscissa(float x, Context * context, Expression::AngleUnit angleUnit) const { Symbol xSymbol = Symbol('x'); Float e = Float(x); context->setExpressionForSymbolName(&e, &xSymbol); - return m_expression->approximate(*context); + return m_expression->approximate(*context, angleUnit); } -float Function::approximateDerivative(float x, Context * context) const { +float Function::approximateDerivative(float x, Context * context, Expression::AngleUnit angleUnit) const { Float abscissa = Float(x); Expression * args[2] = {m_expression, &abscissa}; Derivative derivative = Derivative(); derivative.setArgument(args, 2, true); - return derivative.approximate(*context); + return derivative.approximate(*context, angleUnit); } } diff --git a/apps/graph/function.h b/apps/graph/function.h index 26884032e..8438d0a67 100644 --- a/apps/graph/function.h +++ b/apps/graph/function.h @@ -23,8 +23,8 @@ public: void setDisplayDerivative(bool display); void setContent(const char * c); void setColor(KDColor m_color); - float evaluateAtAbscissa(float x, Context * context) const; - float approximateDerivative(float x, Context * context) const; + float evaluateAtAbscissa(float x, Context * context, Expression::AngleUnit angleUnit) const; + float approximateDerivative(float x, Context * context, Expression::AngleUnit angleUnit) const; private: constexpr static float k_epsilon = 0.0001f; constexpr static float k_precision = 0.01f; diff --git a/apps/graph/graph/goto_parameter_controller.cpp b/apps/graph/graph/goto_parameter_controller.cpp index 5aa927420..4e904f962 100644 --- a/apps/graph/graph/goto_parameter_controller.cpp +++ b/apps/graph/graph/goto_parameter_controller.cpp @@ -1,5 +1,6 @@ #include "goto_parameter_controller.h" #include "../app.h" +#include "../../apps_container.h" #include namespace Graph { @@ -25,7 +26,8 @@ float GoToParameterController::parameterAtIndex(int index) { void GoToParameterController::setParameterAtIndex(int parameterIndex, float f) { assert(parameterIndex == 0); App * graphApp = (Graph::App *)app(); - float y = m_function->evaluateAtAbscissa(f, graphApp->localContext()); + AppsContainer * container = (AppsContainer *)graphApp->container(); + float y = m_function->evaluateAtAbscissa(f, graphApp->localContext(), container->preferences()->angleUnit()); m_graphRange->centerAxisAround(CurveViewRange::Axis::X, f); m_graphRange->centerAxisAround(CurveViewRange::Axis::Y, y); m_cursor->moveTo(f, y); diff --git a/apps/graph/graph/graph_controller.cpp b/apps/graph/graph/graph_controller.cpp index 097903113..328b03844 100644 --- a/apps/graph/graph/graph_controller.cpp +++ b/apps/graph/graph/graph_controller.cpp @@ -41,6 +41,8 @@ void GraphController::didBecomeFirstResponder() { if (m_view.context() == nullptr) { App * graphApp = (Graph::App *)app(); m_view.setContext(graphApp->localContext()); + AppsContainer * container = (AppsContainer *)graphApp->container(); + m_view.setPreferences(container->preferences()); } InteractiveCurveViewController::didBecomeFirstResponder(); } @@ -50,6 +52,7 @@ bool GraphController::didChangeRange(InteractiveCurveViewRange * interactiveCurv return false; } App * graphApp = (Graph::App *)app(); + AppsContainer * myContainer = (AppsContainer *)graphApp->container(); if (m_functionStore->numberOfActiveFunctions() <= 0) { return false; } @@ -63,7 +66,7 @@ bool GraphController::didChangeRange(InteractiveCurveViewRange * interactiveCurv float y = 0.0f; for (int i = 0; i <= Ion::Display::Width; i++) { float x = xMin + i*step; - y = f->evaluateAtAbscissa(x, graphApp->localContext()); + y = f->evaluateAtAbscissa(x, graphApp->localContext(), myContainer->preferences()->angleUnit()); if (!isnan(y) && !isinf(y)) { min = min < y ? min : y; max = max > y ? max : y; @@ -127,7 +130,7 @@ void GraphController::reloadBannerView() { buffer[0] = f->name()[0]; buffer[1] = '\''; App * graphApp = (Graph::App *)app(); - float y = f->approximateDerivative(m_cursor.x(), graphApp->localContext()); + float y = f->approximateDerivative(m_cursor.x(), graphApp->localContext(), myContainer->preferences()->angleUnit()); Float(y).convertFloatToText(buffer + legendLength, Float::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, myContainer->preferences()->displayMode()); m_bannerView.setLegendAtIndex(buffer, 2); } @@ -143,11 +146,12 @@ void GraphController::initCursorParameters() { float x = (m_graphRange.xMin()+m_graphRange.xMax())/2.0f; m_indexFunctionSelectedByCursor = 0; App * graphApp = (Graph::App *)app(); + AppsContainer * myContainer = (AppsContainer *)graphApp->container(); int functionIndex = 0; float y = 0; do { Function * firstFunction = m_functionStore->activeFunctionAtIndex(functionIndex++); - y = firstFunction->evaluateAtAbscissa(x, graphApp->localContext()); + y = firstFunction->evaluateAtAbscissa(x, graphApp->localContext(), myContainer->preferences()->angleUnit()); } while (isnan(y) && functionIndex < m_functionStore->numberOfActiveFunctions()); m_cursor.moveTo(x, y); m_graphRange.panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); @@ -159,7 +163,8 @@ bool GraphController::moveCursorHorizontally(int direction) { xCursorPosition - m_graphRange.xGridUnit()/k_numberOfCursorStepsInGradUnit; Function * f = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); App * graphApp = (Graph::App *)app(); - float y = f->evaluateAtAbscissa(x, graphApp->localContext()); + AppsContainer * myContainer = (AppsContainer *)graphApp->container(); + float y = f->evaluateAtAbscissa(x, graphApp->localContext(), myContainer->preferences()->angleUnit()); m_cursor.moveTo(x, y); m_graphRange.panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); return true; @@ -168,12 +173,13 @@ bool GraphController::moveCursorHorizontally(int direction) { bool GraphController::moveCursorVertically(int direction) { Function * actualFunction = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); App * graphApp = (Graph::App *)app(); - float y = actualFunction->evaluateAtAbscissa(m_cursor.x(), graphApp->localContext()); + AppsContainer * myContainer = (AppsContainer *)graphApp->container(); + float y = actualFunction->evaluateAtAbscissa(m_cursor.x(), graphApp->localContext(), myContainer->preferences()->angleUnit()); Function * nextFunction = actualFunction; float nextY = direction > 0 ? FLT_MAX : -FLT_MAX; for (int i = 0; i < m_functionStore->numberOfActiveFunctions(); i++) { Function * f = m_functionStore->activeFunctionAtIndex(i); - float newY = f->evaluateAtAbscissa(m_cursor.x(), graphApp->localContext()); + float newY = f->evaluateAtAbscissa(m_cursor.x(), graphApp->localContext(), myContainer->preferences()->angleUnit()); bool isNextFunction = direction > 0 ? (newY > y && newY < nextY) : (newY < y && newY > nextY); if (isNextFunction) { m_indexFunctionSelectedByCursor = i; diff --git a/apps/graph/graph/graph_view.cpp b/apps/graph/graph/graph_view.cpp index 8d85f9d14..79026708a 100644 --- a/apps/graph/graph/graph_view.cpp +++ b/apps/graph/graph/graph_view.cpp @@ -5,10 +5,12 @@ namespace Graph { -GraphView::GraphView(FunctionStore * functionStore, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, ::BannerView * bannerView, View * cursorView) : +GraphView::GraphView(FunctionStore * functionStore, InteractiveCurveViewRange * graphRange, + CurveViewCursor * cursor, ::BannerView * bannerView, View * cursorView) : CurveView(graphRange, cursor, bannerView, cursorView), m_functionStore(functionStore), - m_context(nullptr) + m_context(nullptr), + m_preferences(nullptr) { } @@ -29,6 +31,10 @@ void GraphView::setContext(Context * context) { m_context = context; } +void GraphView::setPreferences(Preferences * preferences) { + m_preferences = preferences; +} + Context * GraphView::context() const { return m_context; } @@ -39,7 +45,7 @@ char * GraphView::label(Axis axis, int index) const { float GraphView::evaluateModelWithParameter(Model * curve, float abscissa) const { Function * f = (Function *)curve; - return f->evaluateAtAbscissa(abscissa, m_context); + return f->evaluateAtAbscissa(abscissa, m_context, m_preferences->angleUnit()); } } diff --git a/apps/graph/graph/graph_view.h b/apps/graph/graph/graph_view.h index 5ada72f3c..43fc3dfb7 100644 --- a/apps/graph/graph/graph_view.h +++ b/apps/graph/graph/graph_view.h @@ -6,14 +6,17 @@ #include "../../constant.h" #include "../function_store.h" #include "../../interactive_curve_view_range.h" +#include "../../preferences.h" namespace Graph { class GraphView : public CurveView { public: - GraphView(FunctionStore * functionStore, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, ::BannerView * bannerView, View * cursorView); + GraphView(FunctionStore * functionStore, InteractiveCurveViewRange * graphRange, + CurveViewCursor * cursor, ::BannerView * bannerView, View * cursorView); void drawRect(KDContext * ctx, KDRect rect) const override; void setContext(Context * context); + void setPreferences(Preferences * preferences); Context * context() const; private: char * label(Axis axis, int index) const override; @@ -22,6 +25,7 @@ private: char m_yLabels[k_maxNumberOfYLabels][Float::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)]; FunctionStore * m_functionStore; Context * m_context; + Preferences * m_preferences; }; } diff --git a/apps/graph/values/values_controller.cpp b/apps/graph/values/values_controller.cpp index 0e4b829fb..7fec1a15f 100644 --- a/apps/graph/values/values_controller.cpp +++ b/apps/graph/values/values_controller.cpp @@ -184,9 +184,9 @@ void ValuesController::willDisplayCellAtLocation(TableViewCell * cell, int i, in App * graphApp = (Graph::App *)app(); float x = m_interval.element(j-1); if (isDerivativeColumn(i)) { - Float(function->approximateDerivative(x, graphApp->localContext())).convertFloatToText(buffer, Float::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, myContainer->preferences()->displayMode()); + Float(function->approximateDerivative(x, graphApp->localContext(), myContainer->preferences()->angleUnit())).convertFloatToText(buffer, Float::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, myContainer->preferences()->displayMode()); } else { - Float(function->evaluateAtAbscissa(x, graphApp->localContext())).convertFloatToText(buffer, Float::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, myContainer->preferences()->displayMode()); + Float(function->evaluateAtAbscissa(x, graphApp->localContext(), myContainer->preferences()->angleUnit())).convertFloatToText(buffer, Float::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, myContainer->preferences()->displayMode()); } myValueCell->setText(buffer); } diff --git a/apps/probability/calculation_controller.cpp b/apps/probability/calculation_controller.cpp index 4efbf78fe..c6edc1306 100644 --- a/apps/probability/calculation_controller.cpp +++ b/apps/probability/calculation_controller.cpp @@ -204,7 +204,7 @@ bool CalculationController::textFieldDidReceiveEvent(TextField * textField, Ion: bool CalculationController::textFieldDidFinishEditing(TextField * textField, const char * text) { AppsContainer * appsContainer = (AppsContainer *)app()->container(); Context * globalContext = appsContainer->globalContext(); - float floatBody = Expression::parse(text)->approximate(*globalContext); + float floatBody = Expression::parse(text)->approximate(*globalContext, appsContainer->preferences()->angleUnit()); m_calculation->setParameterAtIndex(floatBody, m_highlightedSubviewIndex-1); for (int k = 0; k < m_calculation->numberOfParameters(); k++) { m_contentView.willDisplayEditableCellAtIndex(k); diff --git a/apps/range_parameter_controller.cpp b/apps/range_parameter_controller.cpp index a09befe5b..1a967b0eb 100644 --- a/apps/range_parameter_controller.cpp +++ b/apps/range_parameter_controller.cpp @@ -34,7 +34,7 @@ void RangeParameterController::willDisplayCellForIndex(TableViewCell * cell, int bool RangeParameterController::textFieldDidFinishEditing(TextField * textField, const char * text) { AppsContainer * appsContainer = (AppsContainer *)app()->container(); Context * globalContext = appsContainer->globalContext(); - float floatBody = Expression::parse(text)->approximate(*globalContext); + float floatBody = Expression::parse(text)->approximate(*globalContext, appsContainer->preferences()->angleUnit()); setParameterAtIndex(m_selectableTableView.selectedRow(), floatBody); willDisplayCellForIndex(m_selectableTableView.cellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()), activeCell());