Quantcast
Channel: NCalc - Mathematical Expressions Evaluator for .NET
Viewing all 58 articles
Browse latest View live

Created Unassigned: get result of formula when values are dynamic/Not Present using Ncalc? [28349]

$
0
0
User can input the formula like this:

1)a+b+c

2)a+b

3)a/b+2+d-8/f

4)a/b

5)and many more

Scenario: Values of all the variables are calculated at run time. For first formula I can set the values 0 if there is no value for a,b or c, but for third formula I can not do the same because ("a/b") it will throw "divide by zero" exception. Formulas and their values are dynamic.

Thanks

Created Unassigned: Constant string (escaped) to Function. [28350]

$
0
0
I have a function that I would like to evaluate: RegEx(test, <re>) where re is the string representation of the regular expression that I would like to evaluate. I cannot seem to find a way to pass a regular expression to the "function".
I first started out with something like
const string skipPageExpression = "RegEx(test,'\\d{4}')";
But this gives me errors (HasErrors()).
Ideas?



New Post: Getting list of variables in an expression...

$
0
0
Be careful, I don't think using EvaluateParameter and EvaluateFunction delegates to record the name will work reliably for a Logical/Binary expression since the expression evaluator uses short circuit evaluation. So if you have and expression like: "(A > 10) && (B < 5)" and if you give it a dummy "A" value that's > 10, then it won't evaluate the "(B < 5)" and the EvaluateParameter call won't be made for "(B < 5)" so you won't get the "B" parameter. In this situation I think "viklele" is the right approach (not sure if it covers all cases).

Created Unassigned: Max undefined? [28351]

$
0
0
I have defined alot of custom functions but I was hoping to also use some of the built in functions. Right now it seems that t'Max" is an invalid function. Any ideas on how to get it recognized? My function resolver looks like:
expression.EvaluateFunction += delegate(string name, FunctionArgs args)
{
switch (name)
{
.
.
.
case "IsNull":
args.Result = args.Parameters[0].Evaluate() == null;
break;
.
.
.
. default:
throw new Exception(string.Format("Invalid function name [{0}].", name));
}
}


New Post: Array of function evaluators or a way to speed up evaluation

$
0
0
Hi,

my extended funciton evaluator is of the form

private void myEvaluator(string name, FunctionArgs args)
{
if(name=="A") { // call function to evaluate }
else if(name=="B") { // call function to evaluate }
//etc
}

This works fine but the problem I'm having is that I use the extended function within NinjaTrader which is a trading platform. The evaluator is called for every tick change, and my functions "A, "B", etc... are now up to about 30 and I'm having real performance issues.

This means that for every evaluation I'm doing up to 30 string comparisons just to get to the correction evaluator function. I would like to use either delegates or function pointers so that I can pass the first parameter in args as a function value or pointer, so that I could have an array of delegates or an array of function pointers (preferably because its simpler) so that I could call the correct evaluator function using that parameter.

I've tried various things for both delegates and function pointers but so far I've ended up with compile or runtime errors. There are a couple of restrictions -
  • putting the evaluator function or delegate in a class won't work, because of the way Ninjatrader variables are initialised.
  • I doubt that I can use static functions because the evaluator needs to access non-static variables.
Has anyone else tried this or are there any examples of how I might do this?

Thanks,
Will.

Commented Unassigned: Constant string (escaped) to Function. [28350]

$
0
0
I have a function that I would like to evaluate: RegEx(test, <re>) where re is the string representation of the regular expression that I would like to evaluate. I cannot seem to find a way to pass a regular expression to the "function".
I first started out with something like
const string skipPageExpression = "RegEx(test,'\\d{4}')";
But this gives me errors (HasErrors()).
Ideas?



Comments: I'm trying to do the exact same thing, but can't get it to work. If you find any way to pass the Regex string as function parameter, let us know! :)

Commented Unassigned: Constant string (escaped) to Function. [28350]

$
0
0
I have a function that I would like to evaluate: RegEx(test, <re>) where re is the string representation of the regular expression that I would like to evaluate. I cannot seem to find a way to pass a regular expression to the "function".
I first started out with something like
const string skipPageExpression = "RegEx(test,'\\d{4}')";
But this gives me errors (HasErrors()).
Ideas?



Comments: Okay, I now found a way: My Regex Custom function looks as follows (note how string expression is pulled from the arguments): ``` if (name == "RegEx") { var value = args.Parameters[0].Evaluate(); string expression = ((NCalc.Domain.ValueExpression)(args.Parameters[1].ParsedExpression)).Value.ToString(); Regex regex = new Regex(@expression); Match match = regex.Match(value.ToString()); args.Result = match.Success; } ``` I can now pass any Regex when all the backslashes are escaped. Example Email RegEx('test@test.com','^([\\w\\.\\-_]+)?\\w+@[\\w-_]+(\\.\\w+){1,}$') Hope this helped you!

New Post: TimeSpan value type?

$
0
0
I know you can define a DateTime object like this #2008/01/31#. I want to know if there's the equivalent to define a TimeSpan value?

For example, I would like to know the amount of time something was offline in a day and get the result as a TimeSpan .NET object :

Result = #24h# - [TimeOnline]

Thanks

New Post: Decimal parameters and float constants

$
0
0
I have the exact same issue!

(X + Y) * 2.23

will fail, if you pass X and Y as decimal parameters!

WHY?!

New Post: Casting to double fails

$
0
0
The code sample in the first post of this thread will also fail, even after your suggested 'fix', if instead of "2" you use "3.5" (for example).

i.e.
string formel = "x+3.5";
Expression e = new Expression(formel);
e.Parameters["x"] = 2.5;

var result = (decimal) e.Evaluate();

will fail with "Operator '+' can't be applied to operands of types 'decimal' and 'double'"

https://ncalc.codeplex.com/discussions/528933

Created Unassigned: Remove debugging call to improve performance [28352]

$
0
0
This is the line that I am referring to. I do not think it is appropriate in a library.

https://github.com/tyrone-sudeium/NCalc/blob/c141185099082663e5c7651e6b87823c9887feb3/Evaluant.Calculator/Expression.cs#L155

I believe that it should be removed

Created Unassigned: Allow to configure CultureInfo [28353]

$
0
0
I see many posts here about dots and comma as the value separator.

From what I see, it use the current computer cultureinfo. I have a french Windows (,), and work with numbers written in English (.). When sending decimal numbers to NCalc, if the input string is using a dot instead of the comma it crash:

System.FormatException: Le format de la chaîne d'entrée est incorrect.
à System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
à System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
à System.Decimal.Parse(String s)
à NCalc.Numbers.ConvertIfString(Object s)
à NCalc.Numbers.Divide(Object a, Object b)
à NCalc.Domain.EvaluationVisitor.Visit(BinaryExpression expression)
à NCalc.Domain.BinaryExpression.Accept(LogicalExpressionVisitor visitor)
à NCalc.Domain.EvaluationVisitor.<>c__DisplayClass2.<Visit>b__1()
à NCalc.Domain.EvaluationVisitor.Visit(BinaryExpression expression)
à NCalc.Domain.BinaryExpression.Accept(LogicalExpressionVisitor visitor)
à NCalc.Expression.Evaluate()

If I test in a separate project, I can easily reproduce it:
var test = Decimal.Parse("12.3"); <-- crash
var test = Decimal.Parse("12.3", CultureInfo.InvariantCulture); <-- ok

Now, I want to keep using dots in the NCalc strings no matter what the Windows locale is set to, so it would be appreciated to be able to configure it to use the CultureInfo you want that will be used in the NCalc internal functions (in this case, InvariantCulture).

Edited Unassigned: Allow to configure CultureInfo [28353]

$
0
0
I see many posts here about dots and comma as the decimal value separator.

From what I see, it use the current computer cultureinfo. I have a french Windows (,), and work with numbers written in English (.). When sending decimal numbers to NCalc, if the input string is using a dot instead of the comma it crash:

System.FormatException: Le format de la chaîne d'entrée est incorrect.
à System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
à System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
à System.Decimal.Parse(String s)
à NCalc.Numbers.ConvertIfString(Object s)
à NCalc.Numbers.Divide(Object a, Object b)
à NCalc.Domain.EvaluationVisitor.Visit(BinaryExpression expression)
à NCalc.Domain.BinaryExpression.Accept(LogicalExpressionVisitor visitor)
à NCalc.Domain.EvaluationVisitor.<>c__DisplayClass2.<Visit>b__1()
à NCalc.Domain.EvaluationVisitor.Visit(BinaryExpression expression)
à NCalc.Domain.BinaryExpression.Accept(LogicalExpressionVisitor visitor)
à NCalc.Expression.Evaluate()

If I test in a separate project, I can easily reproduce it:
var test = Decimal.Parse("12.3"); <-- crash
var test = Decimal.Parse("12.3", CultureInfo.InvariantCulture); <-- ok

Now, I want to keep using dots in the NCalc strings no matter what the Windows locale is set to, so it would be appreciated to be able to configure it to use the CultureInfo you want that will be used in the NCalc internal functions (in this case, InvariantCulture).

New Post: Adding a Parameter in EvaluateParameter delegate

$
0
0
Hi, is there any way I can do something like this:
expression.EvaluateParameter += delegate(string name, ParameterArgs args)
{
    // get hold of the parameter
    string myParam = "[Sub] * [Expression]";

    args.Result = new Expression(myParam);
}
I've also tried
expression.EvaluateParameter += delegate(string name, ParameterArgs args)
{
    // get hold of the parameter
    string myParam = "[Sub] * [Expression]";

    expression.Parameters.Add(name, new Expression(myParam));
}
Neither of those work. Can this be made possible?

New Post: Override ^ operator

$
0
0
I am using NCalc successfully. However, I would like to override the meaning of the ^ operator. Currently it is doing bitwise XOR operation. I want to change it to Math.Pow. Is it possible to do that without digging through the source code ?

Created Unassigned: Adding a Parameter in the EvaluateParameter delegate [28354]

$
0
0
Hi, can we have the ability to add new parameters to the the expression when the EvaluateParameter delegate is running. For example:
```
expression.EvaluateParameter += delegate(string name, ParameterArgs args)
{
// get hold of the parameter
string myParam = "[Sub] * [Expression]";

args.Result = new Expression(myParam);
}
```
I've also tried
```
expression.EvaluateParameter += delegate(string name, ParameterArgs args)
{
// get hold of the parameter
string myParam = "[Sub] * [Expression]";

expression.Parameters.Add(name, new Expression(myParam));
}
```
Neither of those work. Can this be made possible?

At the moment I'm using a LogicalExpressionVisitor to find all parameters needed in the calculation and all the calculation parameters (recursively), and I then look these up and add them as parameters. The parameters are currently public properties on a plain object.

Created Unassigned: IsParameterDefined Operator [28355]

$
0
0
I'd like to propose a new operator that I'm calling IsParameterDefined. I'd like to use it with an if() in the following way:

if (IsParameterDefined([SomeParameterName]), [SomeParameterName] * 5, 0)

if (IsParameterDefined([Param1]) and IsParameterDefined([Param2]), [Param1] * 5 + [Param2] * 3, 0)





New Post: NCalc.dll has failed the NXCheck check

$
0
0
Hi,

I got the following warning when running my application through the Windows Certification program:

WARNING 3
Binary analyzer
Warning: The binary analyzer test detected the following errors:
File C:\Program Files (x86)\Streets Heaver\Compucare8\NCalc.dll has failed the NXCheck check.
Impact if not fixed: If the app doesn’t use the available Windows protections, it can increase the vulnerability of the customer's computer to malware.
How to fix: Apply the SAFESEH, DYNAMICBASE, and NXCOMPAT options when you link the app. See link below for more information:
Fixing Binary Analyzer Errors

Any ideas how to resolve this issue?

Thanks,
James

Commented Unassigned: Remove debugging call to improve performance [28352]

$
0
0
This is the line that I am referring to. I do not think it is appropriate in a library.

https://github.com/tyrone-sudeium/NCalc/blob/c141185099082663e5c7651e6b87823c9887feb3/Evaluant.Calculator/Expression.cs#L155

I believe that it should be removed
Comments: I concur. I'm using it in batch processing some data and it is very annoying, as it litters the Output window with lots of __Expression retrieved from cache__ and __Expression added to cache__ lines, making it very difficult to find my own output messages or error messages. A property capable of turning these on or off would suffice (off by default). Since there are no reliable means to filter the Output window, this is a must have.

Created Unassigned: nCalcExpression.EvaluateFunction += removes support of the pre-existing if() function [28356]

$
0
0
nCalcExpression.EvaluateFunction += removes support of the pre-existing if() function

For example:

var nCalcExpression = GetNCalcExpression("if(1<2,'true','false')");
nCalcExpression.EvaluateFunction += NCalcExtensions.NCalcExtensionFunctions;
var resultObject = nCalcExpression.Evaluate();
var resultString = resultObject.ToString();

This will result in an exception, commenting out the second line would not.

The workaround is to add the following to your static NCalcExtensions.NCalcExtensionFunctions method:

public static void NCalcExtensionFunctions(string functionName, FunctionArgs functionArgs)
{
string param1;
string param2;
switch (functionName)
{
case "if":
bool boolParam1;
object objectParam1;
object objectParam2;
try
{
boolParam1 = (bool)functionArgs.Parameters[0].Evaluate();
objectParam1 = functionArgs.Parameters[1].Evaluate();
objectParam2 = functionArgs.Parameters[2].Evaluate();
}
catch (Exception)
{
throw new MacroParameterException(FunctionConst, "if() requires three parameters.");
}
functionArgs.Result = boolParam1 ? objectParam1 : objectParam2;
return;

... other cases

}
}


Viewing all 58 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>