How to handle expression with 3 decimals?

Jun 5, 2009 at 12:22 PM


Ran into problems evaluating expressions with three decimals...

ExpressionContext context = new ExpressionContext();
IGenericExpression<bool> eGeneric = context.CompileGeneric<bool>("302,129=297,073+5,056");
eGeneric.Evaluate() //<-- returns false

ExpressionContext context = new ExpressionContext();
IGenericExpression<bool> eGeneric = context.CompileGeneric<bool>("302,13=297,07+5,06");
eGeneric.Evaluate() //<-- returns true

"297,073+5,056" returns 302.12899999999996

"297,07+5,06" returns 302.13

My guess is that some implicit type cast is done which gives float values.

How can this be fixed?


Jun 9, 2009 at 4:22 AM

If you declare those numbers as doubles in code and add them, you get the same result so it looks like it's a floating-point math thing.  Changing the values to decimals fixes the problem.  While Flee supports decimal variables, it doesn't support decimal literals.

One solution would be to have an option to specify the data type that should be used to represent real literals so you could choose between float, double, and decimal.

Jun 11, 2009 at 5:46 PM


How big is the problem to support Decimal literals pls ?

Jun 12, 2009 at 7:22 PM

I can probably have it in for the next release (which should be in a week or two).

Do you think I should implement it so that it has to be explicitly specified with a suffix like in C# (ie: 123.44M + 456.56M)?

Or just make it implicit based on a compile option:

context.RealLiteralType = Decimal

"123.44 + 456.56"

Jun 13, 2009 at 2:58 PM


That's great news. I would prefer the explicit (C#) way ...but it can be bice to have RealLiteralType option for default type used when literal is no marked the other way...

Looking forward to nex release ;)

Awesome job btw :)

Jun 15, 2009 at 4:39 PM

I think the implicit option would be a great addition. Very useful library, I'm going to be migrating a current project of mine to use the calculation engine.

Jun 18, 2009 at 3:36 AM

Release now supports decimal literals and the RealLiteralDataType option.

Decimal Literals: 123.45M + 45.6M

RealLiteralDataType option:

context.Options.RealLiteralDataType = RealLiteralDataType.Decimal

now an expression such as "123.45 + 45.6" will evaluate to a Decimal.  You can still override the type by using the d, f, or m suffixes.

Jun 19, 2009 at 8:38 AM

Very nice! Thx