在 mathdotnet 中解析具有双系数值的表达式

Parse expression with double coefficient values in mathdotnet

我正在使用 Infix.Parse 来解析像

这样的方程式
apples + oranges = 2
Expression aleft = Infix.ParseOrThrow("apples + oranges");
Expression aright = Infix.ParseOrThrow("2");

有时我需要解析小数值,比如

2.5*tax + income = 30.5

但这会引发 System.Exception

Error in ...
(2.5)
  ^
Expecting: infix operator or ')'

mathdotnet 中一定有处理此类表达式的方法,但我在文档中找不到。有人可以帮忙吗?

代数表达式可以包含整数和有理数,但不能包含浮点数(无论是在实际代数中还是在 Math.NET 符号中)。您可以使用 symbol until evaluation 或使用精确的有理数。

但是,有人可能会争辩说“30.5”可以解释为“305/10”或自动简化后的“61/2”的 shorthand。我们可以考虑扩展解析器以自动执行此解释,也许可以通过设置将其变为 on/off。你能 open an issue on GitHub 吗?谢谢!

因此,要回答实际问题,在我们扩展解析器之前,您必须改用有理数,例如25/10*tax + income,自动简化为 income + (5/2)*tax