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.
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
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
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?To view this discussion on the web visit https://groups.google.com/d/msg/jep-users/-/Dv-Q3RMRPDQJ.
Kind regards,
Marcus
--
You received this message because you are subscribed to the Google Groups "Jep Java Users" group.
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.