Re: How to add/subtract boolean values with the plus/minus operators?

1,050 views
Skip to first unread message

Richard Morris

unread,
Dec 14, 2012, 4:52:38 AM12/14/12
to jep-...@googlegroups.com, gattin...@googlemail.com
Marcus,

Perhaps the easiest thing to do is use the RealEvaluator (com.singularsys.jep.reals.RealEvaluator)

        jep.setComponent(new RealEvaluator());

this is an evaluator which does all its calculations using doubles. This gives some speed improvements and as a bi-product convertes Boolean values to 0.0 and 1.0.  The one problem with this is you can use Strings or Complex numbers.


Alternative you would need to write your own method to implement the Add operation.

First subclass com.singularsys.jep.functions.Add and override the 

    public Object add(Object param1, Object param2) throws EvaluationException

method. You probably would want something like

static class MyAdd extends Add {
    private static final long serialVersionUID = 1L;

    @Override
    public Object add(Object param1, Object param2) throws EvaluationException {

    Object l,r;

    if(param1 instanceof Boolean) {
        l = ((Boolean) param1) ? Double.valueOf(1.0) : Double.valueOf(0.0);
    }
    else
        l = param1;

    if(param2 instanceof Boolean) {
        r = ((Boolean) param2) ? Double.valueOf(1.0) : Double.valueOf(0.0);
    }
    else
        r = param2;

    return super.add(l, r);
    }
}


You then want to set the new PostFixMathCommand for the Add operator
    jep.getOperatorTable().getAdd().setPFMC(new MyAdd());

    jep.setVariable("True",Boolean.TRUE);
    jep.setVariable("False",Boolean.FALSE);
    jep.parse("True + True");
    Object res = jep.evaluate();
    assertEquals(2.0,res);


A third approach would be to subclass com.singularsys.jep.functions.Comparative

public static class MyComparative extends Comparative {

    public MyComparative(int id_in) {
        super(id_in);
    }

    @Override
    public Object eval(Object l, Object r) throws EvaluationException {
         boolean b = this.compare(l, r);
         if(b)
            return Double.valueOf(1.0);
         else
            return Double.valueOf(0.0);
    }

}

Then use this for your comparative operations

jep.getOperatorTable().getEQ().setPFMC(new MyComparative(Comparative.EQ));
jep.getOperatorTable().getNE().setPFMC(new MyComparative(Comparative.NE));
jep.getOperatorTable().getLT().setPFMC(new MyComparative(Comparative.LT));
jep.getOperatorTable().getGT().setPFMC(new MyComparative(Comparative.GT));
jep.getOperatorTable().getLE().setPFMC(new MyComparative(Comparative.LE));
jep.getOperatorTable().getGE().setPFMC(new MyComparative(Comparative.GE));

this means each comparison operation ==, != > etc gives real results, which could then be added.

Rich


On Thursday, December 13, 2012 12:47:48 AM UTC, gattin...@googlemail.com wrote:
Hi folks!

Say I have the following formula: a + b
Variable "a" evaluates to 1, but "b" evaluates to boolean "true".

Is it possible to add those variables so that the result of the formula is 2?

In short I want to treat a boolean true as 1 and a boolean false as 0 if applied to the basic operators + - * /

Can anyone provide me a hint on the this task?

Kind regards,
Marcus

Richard Morris

unread,
Dec 18, 2012, 4:21:36 AM12/18/12
to Jep Java Users, gattin...@googlemail.com
Marcus,

The RealEvaluator only works with the double type and will try and convert any values to double. Strings and Complex cannot be converted to double and exceptions will be throw if these values are used. This loss of functionality is a trade off for the the increased speed, almost double, by using this evaluator.

For the 3rd alternative its converting the result of any comparison to a Double. You could then use the result of that comparison in any normal calculation. So "(3==3)+(4==4)" returns 2. This is quite similar to the way in which C worked, it did not have a unique boolean type instead using the integer constants 0 and 1 to represent true and false. In this case you would need to ensure that no Boolean values occurred in the system, principle using the jep.addVariable command.

Richard


On Mon, Dec 17, 2012 at 11:57 PM, <gattin...@googlemail.com> wrote:
Thank you very much for your excellent answer.

The 1st and the 3rd option raises some questions to me.

Concerning the 1st alternative:
What do you mean by your comment "[...]The one problem with this is you can use Strings or Complex numbers."?

Concerning the 3rd alternative:
As far as I understand this change makes it possible to correctly evaluate something like "true == 1", right? But I think it won´t help me to evaluate "true + true" to the value 2. Or do I miss something here?


Kind regards,
Marcus

--
You received this message because you are subscribed to the Google Groups "Jep Java Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jep-users/-/Dv-Q3RMRPDQJ.

To post to this group, send email to jep-...@googlegroups.com.
To unsubscribe from this group, send email to jep-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jep-users?hl=en.

Reply all
Reply to author
Forward
0 new messages