jep 2.4.1 functions

82 views
Skip to first unread message

nlasy...@gmail.com

unread,
Nov 13, 2018, 3:31:38 AM11/13/18
to Jep Java Users
Does a round function exist in jep 2.4.1?
Is there a list if all functions available in 2.4.1?

Richard Morris

unread,
Nov 13, 2018, 11:18:19 AM11/13/18
to Jep Java Users
There are two slightly different versions of Jep 2.4.1


and


The former does not have a round function as standard, but the latter does. I think there was a problem when migrating away from sourceforge.

So one option is just to use the second repository.  This adds a few other functions like floor.

Alternatively you can add your own function create the class below

and use

JEP.addFunction("round", new Round());



I think thats all you need.



package org.nfunk.jep.function;

import java.lang.Math;
import java.util.*;
import org.nfunk.jep.*;
import org.nfunk.jep.type.*;

/**
 * A PostfixMathCommandI which rounds a number
 * round(pi) finds the closest integer to the argument
 * round(p1,3) rounds the argument to 3 decimal places
 * @author Richard Morris
 *
 */

public class Round extends PostfixMathCommand
{
   
public Round()
   
{
        numberOfParameters
= -1;
   
}
   
   
public void run(Stack inStack)
       
throws ParseException
   
{
        checkStack
(inStack);// check the stack
       
if(this.curNumberOfParameters==1) {
           
Object param = inStack.pop();
            inStack
.push(round(param));//push the result on the inStack
       
}
       
else {
           
Object r = inStack.pop();
           
Object l = inStack.pop();
            inStack
.push(round(l,r));//push the result on the inStack
           
       
}
       
return;
   
}


   
private Object round(Object l, Object r) throws ParseException {
       
if (l instanceof Number && r instanceof Number)
       
{
           
int dp = ((Number)r).intValue();
           
double val = ((Number)l).doubleValue();
           
double mul = Math.pow(10,dp);
           
return new Double(Math.rint(val*mul)/mul);
       
}
       
throw new ParseException("Invalid parameter type");
   
}

   
public Object round(Object param)
       
throws ParseException
   
{
       
if (param instanceof Number)
       
{
           
return new Double(Math.rint(((Number)param).doubleValue()));
       
}

       
throw new ParseException("Invalid parameter type");
   
}

}
Enter code here...


Reply all
Reply to author
Forward
0 new messages