Formula4J
When an expression such as
B = m/h^2
is part of your program and never changes, then you can always implement the expression directly in code.
For example, you can define a method that takes m and h as parameters,
does the math as ordinary hard-coded statements, and then finally returns the value B as a result.
That's easy.
But what happens when the expression isn't known when the program is being written?
That's not so easy, because now you need a general expression parser.
In the simplest cases, or if you have enough time, you might build such a parser yourself.
But you may prefer to use a tool that's already built, tested, and ready to use.
Formula4j is such a tool.
The formula4j Java library has three main benefits:
- speed of development - the API has a simple, straightforward design. It can be understood by any member of your team in a very short time.
- accuracy - you avoid all rounding errors associated with floating-point data types. Such rounding errors are unacceptable in a business context.
- security - you avoid system security problems associated with general eval() exploits (a form of code injection).
Formula4j parses its input using a grammar strictly limited to mathematical expressions.
Other important facts about formula4j:
- it gives you a JDK 5+ API for the run-time evaluation of basic mathematical expressions
- it targets typical business applications, and not scientific applications
- it lets you use functions and variables in your expressions
- it gives you 48 built-in, commonly used functions
- it lets you define new functions in code
- it lets you define new functions in plain text, so end users can extend your program without coding
- it gives you Decimal, a better abstraction than BigDecimal for most business apps
- it uses UTF-8, so you can use Greek letters such as α, β, and so on, in your expressions
- it lets you change (or translate) the names of its built-in functions
What formula4j does not give you:
- no string functions (sorry)
- no deep-level control over its internal parser
- no complex numbers, matrices, fancy-pants abstract algebra, and so on