Flee icon indicating copy to clipboard operation
Flee copied to clipboard

Mixing decimal and double in calculations

Open ogoldes opened this issue 6 years ago • 8 comments

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.

ogoldes avatar Aug 20 '18 23:08 ogoldes

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

Ijwu avatar Aug 21 '18 20:08 Ijwu

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

metoule avatar Jan 10 '19 14:01 metoule

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.

Ijwu avatar Jan 14 '19 01:01 Ijwu

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 [email protected] 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.

ogoldes avatar Jan 14 '19 11:01 ogoldes

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

K-Leon avatar Jan 16 '19 22:01 K-Leon

+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.

paillave avatar Feb 18 '19 02:02 paillave

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;

hunkydoryrepair avatar Mar 19 '21 18:03 hunkydoryrepair

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;

TheOOnewbie avatar Jun 15 '22 11:06 TheOOnewbie