BigDecimal Mode

With Jep 3, it is now possible to evaluate expressions using decimal arithmetic. This allows calculations to be performed with specified accuracy or even unlimited accuracy. If you are running into precision problems or already know that you will require the highest possible precision in your calculations, we recommend using this operation mode.

Rather than using the double type to represent numbers, BigDecimal is used. The BigDecimal class is part of the Java API and is contained in the java.math package. For more information on this class, please read the Sun Javadocs for BigDecimal.

The difference in the accuracy is best shown through an example. When performing multiplication of two numbers of the double type,

    10*0.09 evaluates as 0.8999999999999999.

But when performing the same calculation using decimal arithmetic with the BigDecimal type,

   10*0.09 evaluates as 0.9.

top

How to use BigDecimal mode

Using Jep in BigDecimal mode is simple. Simply create a new Jep instance using the BigDecComponents with:

import com.singularsys.jep.bigDecimals.BigDecComponents;

...

jep = new Jep(new BigDecComponents());

This initializes Jep with a special the BigDecNumberFactory, BigDecOperatorTable and BigDecFunctionTable. By default, the math context is set to unlimited precision. But you can also initialize the components with a different math context. For example, to use 32-bit precision numbers simply use

jep = new Jep(new BigDecComponents(MathContext.DECIMAL32));

For more information about MathContexts refer to the Sun MathContext Javadocs.

top

Supported Operators and Functions

Guaranteeing precision unfortunately comes at the cost of more complex algorithms for basic operators and functions. For this reason, some of the functions and operators available for double arithmetic are not available in the BigDecimal mode.

The supported operators and functions are as follows:

+, -, *, /, ^ (power), % (modulus)

>, <, >=, <=, == (equals)

&& (and), || (or), ! (not)

Just like with Jep in standard mode, you can add your own custom functions.

top

BigDecimal and Strings

By default BigDecimal and Strings cannot be used together but as of release 3.4 this can now be turned on. The following code switches on this facility and adds the standard string functions.

        // Create the BigDecComponents
        BigDecComponents compSet = new BigDecComponents(MathContext.DECIMAL64,true);
        // Create a jep instance
        jep = new Jep(compSet);
        // Add the standard set of string functions
        jep.setComponent(new StringFunctionSet());
        

The above code will allow strings to be concatenated using '+' and compared using '==', '!=', '<', '<=', '>=', '>'. The StringFunctionSet allows the left, right, lower, upper, substring, len, mid, trim.