Re: [Jexl] Problem using BigDecimal divide and compare

2011-05-26 Thread Narendran MC
Thanks Henri.

Sorry couldn't reply earlier. I was on a vacation. Will take a look at this
today.

On Thu, May 26, 2011 at 7:43 AM, henrib hen...@apache.org wrote:

 Also published a new 2.0.2 snapshot in:


 https://repository.apache.org/content/repositories/snapshots/org/apache/commons/commons-jexl/

 Cheers

 --
 View this message in context:
 http://apache-commons.680414.n4.nabble.com/Jexl-Problem-using-BigDecimal-divide-and-compare-tp3510724p3552738.html
 Sent from the Commons - User mailing list archive at Nabble.com.

 -
 To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
 For additional commands, e-mail: user-h...@commons.apache.org




-- 
~Naren


Re: [Jexl] Problem using BigDecimal divide and compare

2011-05-13 Thread Narendran MC
Thanks Henri.

My understanding of MathContext is it allows you to specify the significant
digits.
Here is a related thread --
http://stackoverflow.com/questions/2529526/bigdecimal-division-mathcontext-very-strange-behaviour

So if that understanding is correct, providing a comparison MathContext will
not solve the problem. Probably, a comparison scale, rounding mode,
would help.

Also curious to know about the issue that I mentioned in the equals method
in JexlArithmetic.

Thanks again.



On Thu, May 12, 2011 at 10:54 PM, henrib hen...@apache.org wrote:

 Howdy,
 I'll be looking into it momentarily. I'm not sure I completely see the pb
 with scaling wrt comparison since it seems a precision issue to me (set a
 precision of 2 in your case); may be a comparison MathContext would be more
 appropriate?
 If a solution is urgent, you can always subclass JexlArithmetic in your own
 project and/or additionally submit a patch.
 Cheers

 --
 View this message in context:
 http://apache-commons.680414.n4.nabble.com/Jexl-Problem-using-BigDecimal-divide-and-compare-tp3510724p3519378.html
 Sent from the Commons - User mailing list archive at Nabble.com.

 -
 To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
 For additional commands, e-mail: user-h...@commons.apache.org




-- 
~Naren


[Jexl] Problem using BigDecimal divide and compare

2011-05-09 Thread Narendran MC
*** Re-sending it **

Hello Jexl Users,

First, thanks for developing this, its very useful and simple to use.

I was planning to use it for one of my projects, but I am running into few
issues. I am using the latest build (2.0.2-Snapshot) - the version in which
JexlArithmetic takes MathContext to solve the divide problem. I see couple
of issues:

1. MathContext allows one to control the precision and rounding mode, but
not the scale. Without that comparison using the result any operation
(multiply/divide) would fail.
2. There seems to be bug in JexlArithmetic equals method. The intend seems
to be to use compareTo when BigDecimal, but the code handling the case when
only one of them is BigDecimal. Here is the snippet

} else if (left.getClass().equals(right.getClass())) {
return left.equals(right);
} else if (left instanceof BigDecimal || right instanceof
BigDecimal) {
return toBigDecimal(left).compareTo(toBigDecimal(right)) == 0;
}
So, when both the operands are BigDecimal, it does equals, which is trouble.
The instanceOfBigDecimal check should go above the class equals check.

I feel the bigger problem is any comparison without applying scale would
fail. Here is the example

String expStr1 = result == salary/month * work.percent/100.00;
Expression exp1 = engine.createExpression(expStr1);
JexlContext ctx = new MapContext();
ctx.set(result, new BigDecimal(9958.33));
ctx.set(salary, new BigDecimal(119500.00));
ctx.set(month,  new BigDecimal(12.00));
ctx.set(percent, new BigDecimal(100.00));

This always returns 'false', as the right operand computes
to 9958.33, which is not equals to or compares
to 9958.33.

I think the JexlArithmetic should take the scale and the equals method
should apply the scale on both the left and right operands and then perform
the check.

Let me know, if this doesn't make any sense, and missing something obvious.

-- 
~Naren


-- 
~Naren


[jexl] Problem with the JexlArithmetic - divide and equals

2011-05-08 Thread Narendran MC
Hello Jexl Users,

First, thanks for developing this, its very useful and simple to use.

I was planning to use it for one of my projects, but I am running into few
issues. I am using the latest build (2.0.2-Snapshot) - the version in which
JexlArithmetic takes MathContext to solve the divide problem. I see couple
of issues:

1. MathContext allows one to control the precision and rounding mode, but
not the scale. Without that comparison using the result any operation
(multiply/divide) would fail.
2. There seems to be bug in JexlArithmetic equals method. The intend seems
to be to use compareTo when BigDecimal, but the code handling the case when
only one of them is BigDecimal. Here is the snippet

} else if (left.getClass().equals(right.getClass())) {
return left.equals(right);
} else if (left instanceof BigDecimal || right instanceof
BigDecimal) {
return toBigDecimal(left).compareTo(toBigDecimal(right)) == 0;
}
So, when both the operands are BigDecimal, it does equals, which is trouble.
The instanceOfBigDecimal check should go above the class equals check.

I feel the bigger problem is any comparison without applying scale would
fail. Here is the example

String expStr1 = result == salary/month * work.percent/100.00;
Expression exp1 = engine.createExpression(expStr1);
JexlContext ctx = new MapContext();
ctx.set(result, new BigDecimal(9958.33));
ctx.set(salary, new BigDecimal(119500.00));
ctx.set(month,  new BigDecimal(12.00));
ctx.set(percent, new BigDecimal(100.00));

This always returns 'false', as the right operand computes
to 9958.33, which is not equals to or compares
to 9958.33.

I think the JexlArithmetic should take the scale and the equals method
should apply the scale on both the left and right operands and then perform
the check.

Let me know, if this doesn't make any sense, and missing something obvious.

-- 
~Naren