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 gives you this:
- it gives you a plain, simple API (JDK 5+) 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 helps you avoid common problems related to using floating-point types
- 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
- it avoids certain kinds of security threats, by avoiding any kind of general eval function,
and parsing input according to a grammar limited to mathematical expressions
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