The following are changes from the original TinyExpr C library:
- Compiles as C++20 code.
te_*functions are now wrapped in ate_parserclass.te_interp(),te_compile(), andte_eval()have been replaced withte_parser::compile(),te_parser::evaluate(), andte_parser::set_variables_and_functions().set_variables_and_functions()sets your list of custom functions and variables.compile()compiles and optimizes an expression. Finally,evaluate()will use the already compiled expression and return its result.evaluate()also has an overload that compiles and evaluates an expression in one call.- Variable/function types (e.g.,
TE_FUNCTION0) have been removed; types are now deduced by the compiler. The available flags for variables and functions are now just combinations ofTE_DEFAULT,TE_PURE, andTE_VARIADIC. - Formula parsing is now case insensitive.
- Added support for variadic functions (can accept 1-24 arguments); enabled through the
TE_VARIADICflag. (Refer to theAVERAGE()function intinyexpr.cppfor an example.) - Added support for parsing formulas in non-US format (e.g.,
pow(2,2; 2)instead ofpow(2.2, 2)). Useful for when the program's locale is non-English. (Refer to Example 4 for a demonstration.) te_expris now a derivable base class. This means that you can derive fromte_expr, add new fields to that derived class (e.g., arrays, strings, even other classes) and then use a custom class as an argument to the various function types that accept ate_expr*parameter. The function that you connect can thendynamic_cast<>this argument and use its custom fields, thus greatly enhancing the functionality for these types of functions. (See below for example.)- Added exception support, where exceptions are thrown for situations like providing invalid separators. Calls to
compileandevaluateshould be wrapped intry...catchblocks. - Memory management is handled by the
te_parserclass (you no longer need to callte_free). Also, replacedmalloc/freewithnew/delete. - Stricter type safety; uses
std::variant(instead of unions) that supportdouble,const double*, and 16 specific function signatures (that will work with lambdas or function pointers). Also usesstd::initializerlists (instead of various pointer operations). - Separate enums are now used between
te_exprandstate's types and are more strongly typed. - Added support for C and C++ style comments (
//and/**/). compile()andevaluate()now acceptstd::string_views, meaning that these functions can accept eitherchar*orstd::stringarguments.- Added support for
volatile. - Custom functions and variables can now contain periods in their names.
- Custom functions and variables can now start with an underscore.
- Custom functions and variables can now be removed.
- Added support for custom handlers to resolve unknown variables.
- Added new built-in functions:
and: returns true (i.e., non-zero) if all conditions are true (accepts 1-24 arguments).average: returns the mean for a range of values (accepts 1-24 arguments).bitand: bitwise AND.bitlrotate: bitwise left rotate. Versions of this are available for 8-, 16-, 32-, and 64-bit integers (if supported by the platform).bitlshift: left shift. Negative shift amount arguments (similar to Excel) are supported.bitnot: bitwise NOT. Versions of this are available for 8-, 16-, 32-, and 64-bit integers (if supported by the platform).bitor: bitwise OR.bitrrotate: bitwise right rotate. Versions of this are available for 8-, 16-, 32-, and 64-bit integers (if supported by the platform).bitrshift: right shift. Negative shift amount arguments (similar to Excel) are supported.bitxor: bitwise XOR.cot: returns the cotangent of an angle.combin: alias forncr(), like the Excel function.clamp: constrains a value to a range.cumprinc: returns the cumulative principal paid on a loan between two periods.cumipmt: returns the cumulative interest paid on a loan between two periods.db: returns the depreciation of an asset for a specified period using the fixed-declining balance method.effect: returns the effective annual interest rate, provided the nominal annual interest rate and the number of compounding periods per year.even: returns a value rounded up to the nearest even integer.fact: alias forfac(), like the Excel function.false: returnsfalse(i.e.,0) in a boolean expression.fv: returns the future value of an investment.ipmt: returns the interest portion of a payment for a specified period of an investment.iserr: returns true if an expression evaluates to NaN.iserror: alias foriserr.iseven: returns true if a number is even, false if odd.isna: alias foriserr.isnan: alias foriserr.isodd: returns true if a number is odd, false if even.if: if a value is true (i.e., non-zero), then returns the second argument; otherwise, returns the third argument.ifs: checks up to three conditions, returning the value corresponding to the first met condition.max: returns the maximum of a range of values (accepts 1-24 arguments).maxint: returns the largest integer value that the parser can store.min: returns the minimum of a range of values (accepts 1-24 arguments).mod: returns remainder from a division.na: returnsNaN(i.e., Not-a-Number) in a boolean expression.nan: alias forna.nominal: returns the nominal annual interest rate, provided the effective rate and the number of compounding periods per year.nper: returns the number of periods for an investment.odd: returns a value rounded up to the nearest odd integer.or: returns true (i.e., non-zero) if any condition is true (accepts 1-24 arguments).not: returns logical negation of value.permut: alias fornpr(), like the Excel function.power: alias forpow(), like the Excel function.pmt: returns the periodic payment for an investment or loan based on a constant interest rate, a fixed number of periods, and a present value (like the Excel function).ppmt: returns the principal portion of a payment for a specified period of an investment.pv: returns the present value of an investment, like the Excel function.rand: returns random number between0and1. Note that this implementation uses the Mersenne Twister (mt19937) to generate random numbers.round: returns a number, rounded to a given decimal point. (Decimal point is optional and defaults to0.) Negative number-of-digits arguments (similar to Excel) is supported.sign: returns the sign of a number:1if positive,-1if negative,0if zero.sum: returns the sum of a list of values (accepts 1-24 arguments).sqr: returns a number squared.tgamma: returns gamma function of a specified value.trunc: returns the integer part of a number.true: returnstrue(i.e.,1) in a boolean expression.
- Added new operators:
&logical AND.|logical OR.&&logical AND.||logical OR.=equal to.==equal to.<>not equal to.!=not equal to.<less than.<=less than or equal to.>greater than.>=greater than or equal to.<<left shift operator.>>right shift operator.<<<left (uint64_t) rotation operator.>>>right (uint64_t) rotation operator.**exponentiation (alias for^).~bitwise NOT.
roundnow supports negative number of digit arguments, similar to Excel. For example,ROUND(-50.55,-2)will yield-100.- Custom variables and functions are now stored in a
std::set(which can be easily accessed and updated via the newget_variables_and_functions()/set_variables_and_functions()functions). - Added
is_function_used()andis_variable_used()functions to see if a specific function or variable was used in the last parsed formula. - Added
set_constant()function to find and update the value of a constant (custom) variable by name. (In this context, a constant is a variable mapped to a double value in the parser, rather than mapped to a runtime variable.) - Added
get_constant()function to return the value of a constant (custom) variable by name. - Added
TE_FLOATpreprocessor flag to usefloatinstead ofdoublefor the parser's data type. - Added
TE_LONG_DOUBLEpreprocessor flag to uselong doubleinstead ofdoublefor the parser's data type. - Binary search (i.e.,
std::set) is now used to look up custom variables and functions (small optimization). - You no longer need to specify the number of arguments for custom functions; it will deduce that for you.
- The position of an error when evaluating an expression is now managed by the
te_parserclass and accessible viaget_last_error_position(). - Some parsing errors can provide error messages available via
get_last_error_message(). - The position of aforementioned error is now 0-indexed (not 1-indexed);
te_parser::nposindicates that there was no error. - Added
success()function to indicate if the last parse succeeded or not. - Added
get_result()function to get result from the last call toevaluateorcompile. - Now uses
std::numeric_limitsfor math constants (instead of macro constants). - Replaced C-style casts with
static_cast<>. - Replaced all macros with
constexprs and lambdas. - Replaced custom binary search used for built-in function searching;
std::setis used now. - Now uses
nullptr(instead of0). - All data fields are now initialized.
- Added Doxygen comments.
- Removed
te_print()debug function. - Added
list_available_functions_and_variables()function to display all available built-in and custom functions and variables. - Added
get_expression()function to get the last formula used. - Added
[[nodiscard]]attributes to improve compile-time warnings. - Added
constexprandnoexceptfor C++ optimization.