-
Notifications
You must be signed in to change notification settings - Fork 270
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
Default precision and scale #1
Comments
Thanks alot, I am always happy to hear some applause ;-) The default precision is set through the default MathContext:
According to Java, the DECIMAL32 defines a precision of 7 and a rounding mode of HALF_EVEN, which matches the IEEE 754R Decimal32 format. I think it would be no problem to change the default MathContext to have a precision of 0, I agree with your idea. With precision and scale, I see a bit of a problem, as the Java implementation has IMHO a problem here. |
Thank you for quick response! I've read the code. addFunction(new Function("ROUND", 2) {
@Override
public BigDecimal eval(List<BigDecimal> parameters) {
BigDecimal toRound = parameters.get(1);
int precision = parameters.get(0).intValue();
return toRound.setScale(precision, mc.getRoundingMode());
}
}); It seems that @Test
public void testRounding() {
assertEquals("3.8", new Expression("round(3.78787,1)").eval().toString());
assertEquals("3.788", new Expression("round(3.78787,3)").eval().toString());
assertEquals("3.734", new Expression("round(3.7345,3)").eval().toString());
assertEquals("-3.734", new Expression("round(-3.7345,3)").eval().toString());
assertEquals("-3.79", new Expression("round(-3.78787,2)").eval().toString());
assertEquals("123.79", new Expression("round(123.78787,2)").eval().toString());
} Maybe this is somewhat different understanding of "precision" and "scale". Another problem I found is:
I think in financial applications. scale and rounding are important because money has a minimum unit, while precision seems not that useful. They always require an unlimited precision because they do not want to lose any money. (They control the fractional part and give freedom to the integer part) Anyway, now I can figure out how to use EvalEx -- I'll use 0 precision and define a function I'm not native English speaker, thank you a lot for patience. |
Thanks alot for your input, I will meditate about it, when I find some time :-) |
Hello, I found EvalEx is very useful and I have similar needs of BigDecimal in expression evaluator.
I really appreciate your great work.
Anyway, I found several issues when I tried to use it.
The first one is that
Expression
seems have an unclear default precision.I just use this code to test:
The input and output will be:
1.1234895
=>1.12349
1.1234891
=>1.123489
What I expected is:
1.1234895
=>1.1234895
1.1234891
=>1.1234891
I can get what I expected by adding:
It can solve this problem but I just get confused by the default behavior.
Can
exp.setPrecision(0);
be a default condition?Another issue is that I could not control the scale during calculation.
We can control precision by
setPrecision
andROUND(expression,precision)
but there is no function for scale.How do you think about these?
I'll dig into the code and try to contribute if I have time.
It is very nice of you if you could do some improvement.
Thank you very much!
The text was updated successfully, but these errors were encountered: