NCalc 1.0.5

Mar 7, 2009 at 9:38 AM
I have uploaded a new version for evaluation first. A few changes but I like some feedback before making it the default release.

Added HasErrors() and Error { get; }to check syntax errors before evaluating
Changed ParsedExpression to public
Added a test method for function overriding

I think next release will focus on performance, and maybe a rewrite of the grammar to use the techniques I learnt by implementing NLinq [].
I'm open for all your requests.

Mar 12, 2009 at 8:49 PM
Edited Mar 12, 2009 at 11:06 PM
Hi Sébastien,

Thanks a lot for the new features.

I still have one little problem. It's dealing with NaN scenarios.
Check this:

double p1 = Double.NaN;
double p2 = 1;
Expression ex = new Expression("p1 + p2");
ex.Parameters["p1"] = p1;
ex.Parameters["p2"] = p2;

double x = (double) ex.Evaluate();  -> System.OverflowException!!!

You may think why on earth am I using Double.NaN in my equations. I'm dealing with interval data and I need it to represent missing values (it could be null, but double is a struct).
When I use custom functions, there is no problem at all, because I can replace NaN with 0 or 1 or even return NaN as result when it makes sense.
But, when the NaN is parameter of an operator there is no place to go.

In my application, some equations would require 1 + NaN = 1. On the other hand, 1 / NaN should be always NaN and the same thing with 1 * NaN.
1 + NaN = 1
(1 + NaN) / NaN = NaN
NaN + NaN = 0

So the feature request is a way to configure what would be the result of an operation with NaN as parameter (per expression).

I thought about being able to override the operators so I would have total control, but maybe that's a bit weird.

Well, that's it.

Thanks a lot so far,


Mar 13, 2009 at 3:46 PM
Hi Sébastien,

I took a look in your source code and found the problem.
Actually, it's not a problem, it's just that you are using decimals as base to calculations (which is the best option indeed).

Anyway, I changed the code adding a EvaluateBinaryExpressionHandler in the same fashion as the EvaluateFunctionHandler and voilà! now I can override any binary operation.

Let me know if this change suits you and if you would like to see the code changed.


Aug 4, 2009 at 9:27 PM

I have uploaded a new version but forgot to check this issue in it. I'll do it and upload a new one.

What should be the correct behaviour while dealing with NaN values ? Throw an exception ? Try to recover based on the operator ?