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

Get results of parts of an expression. & odd evaluation results. #39

Open
se5a opened this issue Jan 25, 2022 · 2 comments
Open

Get results of parts of an expression. & odd evaluation results. #39

se5a opened this issue Jan 25, 2022 · 2 comments

Comments

@se5a
Copy link

se5a commented Jan 25, 2022

Asking for two things here, I'm trying to debug some funky results in an expression which is using formulas, ie the string expression:
"Ability('Mass') * 1000000 * TechData('8557acb9-c764-44e7-8ee4-db2c2cebf0bc')"
This is giving me a funky result when the "Ability('Mass') is 8000.

First off, I want to know if I'm able to break the string expression up and display the results of the functions within the whole expression ie:
Ability('Mass') = 8000
TechData('8557acb9-c764-44e7-8ee4-db2c2cebf0bc') = 1
I did see taht I could cast the ParsedExpression into a binary expression and see the Left and Right expressions, but that wasn't quite what I was after, and didn't seem to see a way to get the evaluated results from that. can I get something like that as a list?

Second, any ideas on what might be going wrong with the above expression?
image

image

@se5a
Copy link
Author

se5a commented Jan 26, 2022

So yeah, the problem here seems to be integer overflow.
Mass in this case is a long, the 10000000 could be changed to 1000000.0, but that is really prone to not always happening, and while it makes for an easy quick fix in this particular case, it's not a long term solution.
tried casting Mass to a double where I thought ncalc would care but that didn't seem to fix the problem.
Expresson.Options has an OverflowProtection flag, but that didn't seem to make a difference here either.

@petli
Copy link
Contributor

petli commented Jan 26, 2022

I'd say just do all of the calculations in double, if the latter expressions are in double anyway. (There are of course things to consider with the precision of floating point numbers if you're actually building a real rocket engine based on these calculations, but I never took that course at uni so I can't advice on that 😃)

To play around with expressions, without just breaking them up into string parts and evaluating them directly, you can create a an EvaluationVisitor (https://github.com/ncalc/ncalc/blob/master/src/NCalc/Domain/EvaluationVisitor.cs) and pass it to the Visit() method of the left/right expressions you've identified above, and then access the Result property in the visitor. This is what NCalc is doing when you call the Evaluate method.

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

3 participants