evaluateOn method

double evaluateOn(
  1. String expression,
  2. double evaluationPoint
)

Evaluates the mathematical expression and replaces the x variable with the value of the given evaluationPoint.

If you want to evaluate a simple expression without the x variable, consider using evaluate.

Implementation

double evaluateOn(String expression, double evaluationPoint) {
  if (!_parser.accept(expression)) {
    throw const ExpressionParserException('The given expression cannot be '
        'parsed! Make sure that all operators are supported. Make also sure '
        "that the product of two values explicitly has the '*' symbol.");
  }

  // The evaluator returns 'num' so this cast is safe
  final value = _parser.parse(expression).value(evaluationPoint) as num;

  // NOTE: The following code is safe because 'num' has only 2 subtypes ('int'
  // and 'double'). Since it is a compile-time error for any type other than
  // 'int' or 'double' to attempt to extend or implement 'num', we can safely
  // assume that a 'num' can always be an integer OR a double.
  //
  // See the doc: https://api.dart.dev/stable/2.12.2/dart-core/num-class.html
  if (value is int) {
    // Converting 'int' into 'double'
    return value * 1.0;
  }

  return value as double;
}