Skip to content

Commit

Permalink
Dynamic Expresson fix parsing of Exponential format number (#134)
Browse files Browse the repository at this point in the history
* Dynamic Expresson fix parsing of Exponential format number

* Dynamic Expresson fix parsing of Exponential format number

* extends test cases for exponential number formats

Co-authored-by: Aleksandr Krasnozhen <aleksandr.krasnozhen@acropolium.com>
  • Loading branch information
alexk47 and Aleksandr Krasnozhen authored Feb 24, 2021
1 parent d0fb762 commit 6f7c13c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static Expression Parse(ParserArguments arguments)
private const NumberStyles ParseLiteralNumberStyle = NumberStyles.AllowLeadingSign;
private const NumberStyles ParseLiteralUnsignedNumberStyle = NumberStyles.AllowLeadingSign;
private const NumberStyles ParseLiteralDecimalNumberStyle = NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint;
private const NumberStyles ParseLiteralDoubleNumberStyle = NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent;
private static readonly CultureInfo ParseCulture = CultureInfo.InvariantCulture;

private readonly ParserArguments _arguments;
Expand Down Expand Up @@ -640,7 +641,7 @@ private Expression ParseRealLiteral()
}
else
{
if (double.TryParse(text, ParseLiteralDecimalNumberStyle, ParseCulture, out double d))
if (double.TryParse(text, ParseLiteralDoubleNumberStyle, ParseCulture, out double d))
value = d;
}

Expand Down
21 changes: 21 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,26 @@ public void GitHub_Issue_128()
Assert.Throws<ParseException>(() => target.Eval("1+1*"));
Assert.Throws<ParseException>(() => target.Eval("1+1*'a'"));
}

[Test]
public void GitHub_Issue_133()
{
var interpreter = new Interpreter();

Assert.AreEqual(10000000001, interpreter.Eval("1+1e10"));
Assert.AreEqual(10000000001, interpreter.Eval("1+1e+10"));
Assert.AreEqual(1.0000000001, interpreter.Eval("1+1e-10"));
Assert.AreEqual(-20199999999, interpreter.Eval("1 - 2.02e10"));
Assert.AreEqual(-20199999999, interpreter.Eval("1 - 2.02e+10"));
Assert.AreEqual(0.999999999798, interpreter.Eval("1 - 2.02e-10"));
Assert.AreEqual(1e-10, interpreter.Eval("1/1e+10"));

interpreter.SetVariable("@Var1", 1);
interpreter.SetVariable("@Var2", 1e+10);
Assert.AreEqual(10000000001, interpreter.Eval("@Var1+@Var2"));

interpreter.SetVariable("e", 2);
Assert.AreEqual(10000000003, interpreter.Eval("@Var1+@Var2+e"));
}
}
}
4 changes: 3 additions & 1 deletion test/DynamicExpresso.UnitTest/LiteralsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public void Numeric_Literals()
Assert.AreEqual(.201, target.Eval(".201"));
Assert.AreEqual(-.201, target.Eval("-.201"));
Assert.AreEqual(+.201, target.Eval("+.201"));
Assert.AreEqual(2e+201, target.Eval("2e+201"));
Assert.AreEqual(2e+20, target.Eval("2e+20"));

// f suffix (single)
Assert.AreEqual(4f, target.Eval("4f"));
Expand Down Expand Up @@ -314,7 +316,7 @@ public void Should_Understand_ReturnType_Of_Literals()
Assert.AreEqual((45.5).GetType(), target.Eval("45.5").GetType());
Assert.AreEqual((45.8f).GetType(), target.Eval("45.8f").GetType());
Assert.AreEqual((45.232M).GetType(), target.Eval("45.232M").GetType());

Assert.AreEqual((2e+201).GetType(), target.Eval("2e+201").GetType());
}

[Test]
Expand Down

0 comments on commit 6f7c13c

Please sign in to comment.