Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixing decimal and double in calculations #40

Open
ogoldes opened this issue Aug 20, 2018 · 8 comments
Open

Mixing decimal and double in calculations #40

ogoldes opened this issue Aug 20, 2018 · 8 comments

Comments

@ogoldes
Copy link

ogoldes commented Aug 20, 2018

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
Copy link

Ijwu commented Aug 21, 2018

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

@metoule
Copy link

metoule commented Jan 10, 2019

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

@Ijwu
Copy link

Ijwu commented Jan 14, 2019

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
Copy link
Author

ogoldes commented Jan 14, 2019 via email

@K-Leon
Copy link

K-Leon commented Jan 16, 2019

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

@paillave
Copy link

+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
Copy link
Contributor

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
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants