Note that you can always add new custom functions. Each of the following functions can be applied to objects of the types indicated.
Functions added with addStandardFunctions():Double | Complex | String | Vector | ||
Sine | sin(x) | ||||
Cosine | cos(x) | ||||
Tangent | tan(x) | ||||
Arc Sine | asin(x) | ||||
Arc Cosine | acos(x) | ||||
Arc Tangent | atan(x) | ||||
Arc Tangent (with 2 parameters) | atan2(y, x) | ||||
Hyperbolic Sine | sinh(x) | ||||
Hyperbolic Cosine | cosh(x) | ||||
Hyperbolic Tangent | tanh(x) | ||||
Inverse Hyperbolic Sine | asinh(x) | ||||
Inverse Hyperbolic Cosine | acosh(x) | ||||
Inverse Hyperbolic Tangent | atanh(x) | ||||
Natural Logarithm | ln(x) | ||||
Logarithm base 10 | log(x) | ||||
Exponential (e^x) | exp(x) | ||||
Absolute Value / Magnitude | abs(x) | ||||
Random number (between 0 and 1) | rand() | ||||
Modulus | mod(x,y) = x % y | ||||
Square Root | sqrt(x) | ||||
Sum | sum(x,y,z) | ||||
If | if(cond,trueval,falseval) | ||||
Str (number to string) | str(x) | ||||
Binomial coefficients | binom(n,i) | Integer values |
Double | Complex | String | Vector | ||
Real Component | re(c) | ||||
Imaginary Component | im(c) | ||||
Complex Modulus (Absolute Value) | cmod(c) | ||||
Argument (Angle of complex value, in radians) | arg(c) | ||||
Complex conjugate | conj(c) | ||||
Complex, constructs a complex number from real and imaginar parts | complex(x,y) | ||||
Polar, constructs a complex number from modulus and argument | polar(r,theta) |
The following is an example of how a custom function can be added.
Assume you want to add a function "half" to divide a number by two (for demonstration purposes).
public void run(Stack inStack) throws ParseException { // check the stack checkStack(inStack); // get the parameter from the stack Object param = inStack.pop(); // check whether the argument is of the right type
if (param instanceof Double) { // calculate the result
double r = ((Double)param).doubleValue() / 2; // push the result on the inStack
inStack.push(new Double(r));
} else {
throw new ParseException("Invalid parameter type"); } }
parser.addFunction("half", new Half());
Source files