commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henrib <>
Subject Re: [JEXL] Evaluating math expression
Date Sun, 27 Nov 2011 16:29:55 GMT
You can use JEXL as a math interpreter but you need to derive JexlArithmetic
to fit your purpose.
this means, create your own JexlArithmeticWithPow class, overload the method
bitwiseXor to perform pow and create your JexlEngine instance with an
instance of that class.
For example:
    public static class WithPow extends
org.apache.commons.jexl2.JexlArithmetic {
        public WithPow(boolean lenient) {

        public Object bitwiseXor(Object left, Object right) {
            double l = toDouble(left);
            double r = toDouble(right);
            return Math.pow(l, r);

    public void test120() throws Exception {
        JexlEngine jexl = new JexlEngine(null, new WithPow(true), null,
        Expression func = jexl.createExpression("(x1-2)^4+(x1-2*x2)^2");
        MapContext mc = new MapContext();
        mc.set("x1", 2);
        mc.set("x2", 1);
        assertEquals(0, func.evaluate(mc));
Hope this helps,

View this message in context:
Sent from the Commons - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message