Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 127 additions & 10 deletions lib/src/math_node.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:math' as math;

import 'package:math_parser/src/math_errors.dart';
import 'math_errors.dart';

/// Variables dictionary
///
Expand Down Expand Up @@ -774,6 +774,14 @@ abstract class MathComparison extends MathExpression {
...right.getUsedFreeformFunctions()
};
}

/// Evaluates the comparison and returns 1 if it is true
/// and 0 otherwise
num? evaluate(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
});
}

/// Equation
Expand All @@ -800,6 +808,27 @@ class MathComparisonEquation extends MathComparison {
return null;
}

@override
num? evaluate(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
);
final rightResult = right.calc(
values,
customFunctions: customFunctions,
);

if (leftResult == rightResult) return 1;

return 0;
}


@override
String toString() {
return '[$left = $right]';
Expand All @@ -816,10 +845,10 @@ class MathComparisonEquation extends MathComparison {
class MathComparisonGreater extends MathComparison {
@override
num? calc(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
Expand All @@ -836,6 +865,28 @@ class MathComparisonGreater extends MathComparison {
return rightResult;
}

@override
num? evaluate(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
);
if (leftResult == null) return null;

final rightResult = right.calc(
values,
customFunctions: customFunctions,
);
if (rightResult == null) return null;

if (leftResult > rightResult) return 1;
return 0;
}

@override
String toString() {
return '[$left > $right]';
Expand Down Expand Up @@ -872,6 +923,28 @@ class MathComparisonGreaterOrEquals extends MathComparison {
return rightResult;
}

@override
num? evaluate(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
);
if (leftResult == null) return null;

final rightResult = right.calc(
values,
customFunctions: customFunctions,
);
if (rightResult == null) return null;

if (leftResult >= rightResult) return 1;
return 0;
}

@override
String toString() {
return '[$left >= $right]';
Expand All @@ -888,10 +961,10 @@ class MathComparisonGreaterOrEquals extends MathComparison {
class MathComparisonLess extends MathComparison {
@override
num? calc(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
Expand All @@ -908,6 +981,28 @@ class MathComparisonLess extends MathComparison {
return rightResult;
}

@override
num? evaluate(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
);
if (leftResult == null) return null;

final rightResult = right.calc(
values,
customFunctions: customFunctions,
);
if (rightResult == null) return null;

if (leftResult < rightResult) return 1;
return 0;
}

@override
String toString() {
return '[$left < $right]';
Expand All @@ -924,10 +1019,10 @@ class MathComparisonLess extends MathComparison {
class MathComparisonLessOrEquals extends MathComparison {
@override
num? calc(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
Expand All @@ -944,6 +1039,28 @@ class MathComparisonLessOrEquals extends MathComparison {
return rightResult;
}

@override
num? evaluate(
MathVariableValues values, {
MathCustomFunctionsImplemented customFunctions =
const MathCustomFunctionsImplemented({}),
}) {
final leftResult = left.calc(
values,
customFunctions: customFunctions,
);
if (leftResult == null) return null;

final rightResult = right.calc(
values,
customFunctions: customFunctions,
);
if (rightResult == null) return null;

if (leftResult <= rightResult) return 1;
return 0;
}

@override
String toString() {
return '[$left <= $right]';
Expand Down