Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public void Interpret_NoResult_WhenCalled(string input)
["log2(3)", 1.58496250072116M],
["log10(3)", 0.47712125471966M],
["ln(e)", 1M],

// Space between function name and '(' must produce the same result
// (regression test for the log-mapping bug).
["ln (3)", 1.09861228866811M],
["log (3)", 0.47712125471966M],
["log2 (3)", 1.58496250072116M],
["log10 (3)", 0.47712125471966M],

["cosh(0)", 1M],
["1*10^(-5)", 0.00001M],
["1*10^(-15)", 0.0000000000000001M],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ public static CalculateResult Interpret(ISettingsInterface settings, string inpu

// mages has quirky log representation
// mage has log == ln vs log10
input = input.
Replace("log(", "log10(", true, CultureInfo.CurrentCulture).
Replace("ln(", "log(", true, CultureInfo.CurrentCulture);
// Use regex replacements so optional whitespace between the function name and
// '(' is handled correctly - "log (100)" must map to log10 just like "log(100)"
// does. The negative lookahead prevents "log10" / "log2" from being touched.
input = LogRegex().Replace(input, "log10(");
input = LnRegex().Replace(input, "log(");

input = CalculateHelper.FixHumanMultiplicationExpressions(input);

Expand Down Expand Up @@ -135,6 +137,15 @@ public static decimal FormatMax15Digits(decimal value, CultureInfo cultureInfo)
return rounded / 1.000000000000000000000000000000000m;
}

[GeneratedRegex("\\/\\s*0(?!(?:[,\\.0-9]|[box]0*[1-9a-f]))", RegexOptions.IgnoreCase, "en-US")]
[GeneratedRegex("\\/\\s*0(?!(?:[,\\.0-9]|[box]0*[1-9a-f]))", RegexOptions.IgnoreCase)]
private static partial Regex DivisionByZeroRegex();

// Case-insensitive match for "log" not followed by a digit, then optional whitespace,
// then '('. The negative lookahead protects "log2" and "log10". A new log variant
// like "logb" must be handled explicitly.
[GeneratedRegex("log(?![0-9])\\s*\\(", RegexOptions.IgnoreCase)]
private static partial Regex LogRegex();

[GeneratedRegex("ln\\s*\\(", RegexOptions.IgnoreCase)]
private static partial Regex LnRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public void Interpret_NoResult_WhenCalled(string input)
new object[] { "log2(3)", 1.58496250072116M },
new object[] { "log10(3)", 0.47712125471966M },
new object[] { "ln(e)", 1M },

// Space between function name and '(' must produce the same result
// (regression test for the log-mapping bug).
new object[] { "ln (3)", 1.09861228866810M },
new object[] { "log (3)", 0.47712125471966M },
new object[] { "log2 (3)", 1.58496250072116M },
new object[] { "log10 (3)", 0.47712125471966M },

new object[] { "cosh(0)", 1M },
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ public CalculateResult Interpret(string input, CultureInfo cultureInfo, out stri

// mages has quirky log representation
// mage has log == ln vs log10
input = input.
Replace("log(", "log10(", true, CultureInfo.CurrentCulture).
Replace("ln(", "log(", true, CultureInfo.CurrentCulture);
// Use regex replacements so optional whitespace between the function name and
// '(' is handled correctly - "log (100)" must map to log10 just like "log(100)"
// does. The negative lookahead prevents "log10" / "log2" from being touched.
input = LogRegex().Replace(input, "log10(");
input = LnRegex().Replace(input, "log(");

input = CalculateHelper.FixHumanMultiplicationExpressions(input);

Expand Down Expand Up @@ -125,7 +127,16 @@ private static dynamic TransformResult(object result)
return result;
}

[GeneratedRegex("\\/\\s*0(?!(?:[,\\.0-9]|[box]0*[1-9a-f]))", RegexOptions.IgnoreCase, "en-US")]
[GeneratedRegex("\\/\\s*0(?!(?:[,\\.0-9]|[box]0*[1-9a-f]))", RegexOptions.IgnoreCase)]
private static partial Regex DivisionByZeroRegex();

// Case-insensitive match for "log" not followed by a digit, then optional whitespace,
// then '('. The negative lookahead protects "log2" and "log10". A new log variant
// like "logb" must be handled explicitly.
[GeneratedRegex("log(?![0-9])\\s*\\(", RegexOptions.IgnoreCase)]
private static partial Regex LogRegex();

[GeneratedRegex("ln\\s*\\(", RegexOptions.IgnoreCase)]
private static partial Regex LnRegex();
}
}
Loading