Package com.singularsys.jep.functions
Class Round
java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.NaryFunction
com.singularsys.jep.functions.Round
- All Implemented Interfaces:
PostfixMathCommandI,Serializable
- Direct Known Subclasses:
BigDecRound,RInt,RoundBD
A PostfixMathCommandI which rounds a number.
round(a) adds 0.5 to the argument and returns the closest integer.round(a,3) rounds the argument to 3 decimal places.
Note: This class was changed in version 3.4. Prior to 3.4, this class used the Math.rint method. In 3.4 and future releases Math.round will be used instead. The effect is changing the rounding behaviour when the input is equally close to two integers, Math.round rounds 1.5 down and Math.rint round it up.
In Jep 4.2 fixes a bug with round(100000,-5) -> 99999.99999999999.
An alternative algorithm is provided by the RoundBD class, this uses BigDecimal's
setscale(scale,mode) method. This can improve results for cases like round(265.335,2)
where the floating-point representation of 265.335 is not exact.
- Author:
- Richard Morris
- See Also:
-
Field Summary
Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, name, NaN, numberOfParameters -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckNumberOfParameters(int n) Must have one or more parameterEvaluate the functionprotected doubleround(double val) The rounding method which implements the core rounding operation used in this class.voidCreates an array of object then callsNaryFunction.eval(Object[])Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, getName, getNumberOfParameters, setCurNumberOfParameters, setName, toString, toString
-
Constructor Details
-
Round
public Round()
-
-
Method Details
-
checkNumberOfParameters
public boolean checkNumberOfParameters(int n) Must have one or more parameter- Specified by:
checkNumberOfParametersin interfacePostfixMathCommandI- Overrides:
checkNumberOfParametersin classPostfixMathCommand- Parameters:
n- number of parameters function will be called with.- Returns:
- false if an illegal number of parameters is supplied, true otherwise.
-
run
Description copied from class:NaryFunctionCreates an array of object then callsNaryFunction.eval(Object[])- Specified by:
runin interfacePostfixMathCommandI- Overrides:
runin classNaryFunction- Parameters:
inStack- arguments for function- Throws:
EvaluationException- if function cannot be evaluated
-
round
- Throws:
EvaluationException
-
round
- Throws:
EvaluationException
-
round
protected double round(double val) The rounding method which implements the core rounding operation used in this class. Other subclasses (such as RInt) can override this to perform custom rounding.- Parameters:
val- number to round- Returns:
- the rounded value
- Since:
- 3.4.0
-
eval
Description copied from class:NaryFunctionEvaluate the function- Specified by:
evalin classNaryFunction- Parameters:
args- arguments to the function- Returns:
- value returned by the function
- Throws:
EvaluationException- if the calculation cannot be performed
-