Home | License | API Docs
Summary | Download
ShiftOne

ShiftOne Big Calculator

What is this?
Why use Big Calculator?
What data types are used?
How do I use this?
Is there an interactive mode?
What guarantees are provided?

What is this?

Big Calculator is a Java library for parsing mathematical and logical expressions into executable functors. These functor instances can be used repeatedly to perform a calculation using Java's arbitrary-precision number types.

Why use Big Calculator?

  • Readability of arbitrary-precision formulae - When using the arbitrary-precision BigDecimal and BigInteger classes, code can become very unreadable. The expressions that Big Calculator uses are much easier to read, understand, and change.
  • Configurablity - Occasionally, it is usefull for application to be able to support configurable formulae or logic expressions. The example comes to mind is a promotion code on an online store. The code could be used to lookup a discount formulae.
  • What data types are used?

    While any subclasses of Number can be "passed in" via the Context, most operations will result in values of type BigDecimal, BigInteger, or Boolean. Generally, values are promoted from BigInteger to BigDecimal only when one of the operands is a BigDecimal, however this is not true for division, which alwayse uses BigDecimal.

    How do I use this?

    Here are a few examples. This one will calculate Pi using Gregory's Formula for arctan. Note that this library has been used to calculate Pi to 100,000 digits.
    String     eq   = "4*(4*arctan(1/5)-arctan(1/239))";
    Functor    calc = new Calculator(eq);
    BigDecimal pi   = calc.evaluate(new StdContext());
    

    This will apply a 10% discount to any total over 200.
    String  eq   = "if (total >= 200) total * 0.9 else total";
    Functor calc = new Calculator(eq);
    
    Context    context = new StdContext();
    context.setScale(3);
    context.setValue("total", 93.5);
    
    BigDecimal bill = functor.evaluate(context);
    

    This demonstrates how data can be shared by different functors using a shared Context instance.
    Functor calcA = new Calculator("a = 5");
    Functor calcB = new Calculator("b = 6");
    Functor calcC = new Calculator("c = 2 * a + b");
    
    Context    context = new StdContext();
    calcA.evaluate(context); // a just got set to 5
    calcB.evaluate(context); // b just got set to 6
    calcC.evaluate(context); // c just got set to 17
    
    BigDecimal c = context.getValue("c"); // 17
    

    Is there an interactive mode?

    Yes. The org.shiftone.bigcalc.Calculator class contains a main() method.

    What guarantees are provided?

    None. Simple operations like plus, minus, multiply and divide use the functionality provided by Java's BigDecimal and BigInteger, and are probably very safe. Other functions, like sin(), cos(), tan() and sqrt() do more complex processing. Most functions that could be were tested and compaired against their java.lang.Math counterparts.

    ShiftOne Big Calculator 0.2

    SourceForge.net Logo
    page regenerated
    © Jeff Drost