All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.datarp.calc.Calculator

java.lang.Object
   |
   +----com.datarp.calc.Calculator

public class Calculator
extends Object
The Calculator class parses and computes the value of arithmetic expressions using the four basic arithmentic opertors and an unlimited number of nested parenthesis. The computations are performed using integer operations only to facilitate floating point operations in environments where the primitive types float and double are unavailable.

For example, the Java expression, new Calculator("(2.01+3.5)*2.2").evaluate() will return the String "12.122".

The Calculator class uses the same order of operations as the Java(TM) language for the operators it supports:

Numeric literals are composed of a positive or negative floating point base optionally followed by the letter e or E and a positive or negative exponent.

Examples of valid literals are:

It is critical to understand the role of precision in performing computations. The primitive data type long is used to hold the base of floating point numbers. As such, there is a finite amount of precision that can be stored internally. Further, a given computation may require additional precision, which limits the amount of initial precision that is available. By default, the precision is set to be half the number of digits found in Long.MAX_VALUE. In standard Java, this is nine digits of precision. All numbers read in will be rounded to the set number of digits of precision, any additional power of ten are transfered to the exponent. Likewise, all results are rounded to the set number of digits of precision.

The default choice of 9 digits of precision gaurantees that the largest integer that can be represented in nine digits times itself will not produce an integer overflow condition. This same claim can be be made for any precision less than nine, AND CANNOT be made for any precision greater than nine.

The default is a good choice for many computations, but NOT for all computations. For example, the sum 1e10 + 1 produces the answer 1e10 if nine digits of precision are used. Increasing the precision to eleven digits produces the answer 10000000001.

As with any floating point computation, it is important to fully understand the manner in which any calculation is made, and to extensively test results for accuracy. The Calculator class is incapable of performing computations with high levels of precision.

The Calculator class explicitly does not perform computations in the same manner as the JVM performs float and double primitive computations, and is not gauranteed to produce identical results, or to round results by any common convention. Further, Data Representations, Inc. expressly states that any answers produced by the Calculator class may be entirely inaccurate or incorrect, and Data Representations, Inc. disclaims any liability that arises from the use of such results.

Version:
1.0, 10/29/00
Author:
Carl H. Sayres, Data Representations, Inc.

Constructor Index

 o Calculator(String)
Creates a new Calculator object with the given expression, with the default precision.
 o Calculator(String, int)
Creates a new Calculator object with the given expression and precision.

Method Index

 o evaluate()
Evaluates the expression.
 o getExpression()
Gets the current expression
 o getPrecision()
Gets the current precision
 o main(String[])
A test routine.
 o setExpression(String)
Sets the expression
 o setPrecision(int)
Sets the precision.

Constructors

 o Calculator
 public Calculator(String expression)
Creates a new Calculator object with the given expression, with the default precision. The default precision is equal to Long.MAX_VALUE/2. In standard Java, this is equal to 9.

 o Calculator
 public Calculator(String expression,
                   int precision)
Creates a new Calculator object with the given expression and precision.

Methods

 o main
 public static void main(String args[])
A test routine. It evaluates an expression (from the args[] array) at different precision levels.

 o setExpression
 public void setExpression(String expression)
Sets the expression

 o setPrecision
 public void setPrecision(int precision)
Sets the precision. The precision must be greater than 1 and less than Long.MAX_VALUE.

 o getExpression
 public String getExpression()
Gets the current expression

 o getPrecision
 public int getPrecision()
Gets the current precision

 o evaluate
 public String evaluate()
Evaluates the expression.

Returns:
a new String with the computed value.

All Packages  Class Hierarchy  This Package  Previous  Next  Index