mparlak / Flee

Fast Lightweight Expression Evaluator
628 stars 120 forks source link

Mixing decimal and double in calculations #40

Open ogoldes opened 6 years ago

ogoldes commented 6 years ago

if a decimal variable like decimal vdec = 10 is used in a formula, evaluating a formula like "vdec 1.23" throws an error : Flee.PublicTypes.ExpressionCompileException: 'ArithmeticElement: Operation 'Multiply' is not defined for types 'Decimal' and 'Double'' I found a workaround changing the formula to "vdec 1.23M", but this is not very nice... Is there any way to force flee to assume all constants are decimal? Note that mixing decimal and int : "vdec * 2" is OK.

Ijwu commented 6 years ago

I'd also like to know if this is a possibility. This would be handy for my use case.

metoule commented 5 years ago

Trying to multiply a decimal with a double is not valid C#, so you need a conversion.

Ijwu commented 5 years ago

That being given, the intent here is to provide a DSL for expressions. It doesn't have to mirror C# perfectly, so much as accommodate useful cases.

Is there any way to force flee to assume all constants are decimal?

This is the real issue at hand. My use case required maintaining floating point integrity (for financial calculations) and so assuming all constants are Decimal would be exceptionally useful.

ogoldes commented 5 years ago

In my use case, I use exclusively decimal calculations, and the some constants are input by the user (formulas are written by the user). So it would be very useful that constants in formulas are assumed to be decimal. Oscar

El 13 ene. 2019, a la(s) 22:35, Hussein Farran notifications@github.com escribió:

That being given, the intent here is to provide a DSL for expressions. It doesn't have to mirror C# perfectly, so much as accommodate useful cases.

Is there any way to force flee to assume all constants are decimal?

This is the real issue at hand. My use case required maintaining floating point integrity (for financial calculations) and so assuming all constants are Decimal would be exceptionally useful.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

K-Leon commented 5 years ago

Having same Issue - anyone found a solution besides adding an m to every numer with a dot in it?

paillave commented 5 years ago

+1 for me... for information, financial and accountancy computations can't possibly done in float. These computations must be done with a precise type like decimal. Otherwise, computations can provide unexpected results like 15.000000000000000000001.

hunkydoryrepair commented 3 years ago

I accomplished this with the Options. Not sure when this was added, but you can default the literals to Decimal using this:

ExpressionContext context = new ExpressionContext();
context.Options.RealLiteralDataType = RealLiteralDataType.Decimal;
TheOOnewbie commented 2 years ago

hello there, I had the same problem, you can use the following code to force the Flee parser to not do any integer division any more, and of course you would take the results from the parser and convert them to decimals

ExpressionContext context = new ExpressionContext(); context.Options.IntegersAsDoubles = true;