diff --git a/TODO.md b/TODO.md index 579bbc0..1107f1e 100644 --- a/TODO.md +++ b/TODO.md @@ -30,4 +30,6 @@ - [X] From now on, the corpus will be loaded from database instead of assets - [X] Create method List Corpus.withDependencies(Formula formula). It will return the formula, the units of the formula, and all the units from the corpus with the same base unit. - [X] Add a Share button to the formula list. It will export the array string literal of the formula with the units from Corpus.withDependencies(). -- [ ] Replace flutter-markdown with flutter-markdown-plus +- [X] Replace flutter-markdown with flutter-markdown-plus +- [X] Heron's formula: investigate why a=3, b=40, c=5 yields NaN. Root cause: input values don't form a valid triangle (violate triangle inequality: 3+5=8 is not > 40). Added documentation note to the formula description. +- [ ] Investigate starup time when there is no previous database and corpus is loaded from assets. diff --git a/assets/formulas/formulas.d4rt b/assets/formulas/formulas.d4rt index e3ced5d..3126df4 100644 --- a/assets/formulas/formulas.d4rt +++ b/assets/formulas/formulas.d4rt @@ -1036,7 +1036,9 @@ Where: - $a, b, c$: Side lengths (meters) - $s$: Semi-perimeter $= \frac{a+b+c}{2}$ -This formula is useful when height is unknown.''', +This formula is useful when height is unknown. + +**Note:** The side lengths must satisfy the triangle inequality: the sum of any two sides must be greater than the third side (a+b>c, a+c>b, b+c>a). If this condition is not met, the formula returns NaN.''', "input": [ {"name": "a", "unit": "meter"}, // Side 1 {"name": "b", "unit": "meter"}, // Side 2 @@ -1044,6 +1046,9 @@ This formula is useful when height is unknown.''', ], "output": {"name": "A", "unit": "square meter"}, // Area "d4rtCode": """ + if( a + b < c || a+c < b || b+c < a ){ + signal( "There is not a valid triangle with those longitudes" ); + } var s = (a + b + c) / 2; A = sqrt(s * (s - a) * (s - b) * (s - c)); """, diff --git a/lib/formula_evaluator.dart b/lib/formula_evaluator.dart index ca2c88b..3aaf68a 100644 --- a/lib/formula_evaluator.dart +++ b/lib/formula_evaluator.dart @@ -162,6 +162,10 @@ class FormulaEvaluator { import "package:d4rt_formulas.dart"; """; + static final String signal = """ + void signal( String msg ) => throw Exception(msg); + """; + static const reservedVariableNames = { "variableValues", "indexOf", "variableAllowedValues"} ; String _buildCompleteSource(Formula formula, Map inputValues) { @@ -169,7 +173,7 @@ class FormulaEvaluator { buffer.writeln(""" $d4rtImports - + $signal main() { """