26 Evaluating mathematical expressions

The GNU Bash has some arithmetic facilities to evaluate expressions, they are limited to integers; See Shell Arithmetic. The GNU Coreutils’s expr command can also evaluate mathematical expressions, its features are also limited; See expr Evaluate expressions.

GAWK instead has a wider set of facilities, so it is used by MBFL for mathematics; See Arithmetic and Arbitrary–Precision Arithmetic with gawk. The pathname of GAWK is available in the variable mbfl_PROGRAM_GAWK (see mbfl_PROGRAM_GAWK).

Function: mbfl_math_expr EXPR_STRING

Evaluate the given expression string using GAWK; print the result to stdout. If an error occurs: return with the same status of GAWK.

mbfl_math_expr '1.2 + 3.4'
-| 4.6
Function: mbfl_math_expr_var RV EXPR_STRING

Evaluate the given expression string using GAWK; only if successful: store the result in the variable RV. If an error occurs: return with the same status of GAWK; RV is left untouched.

mbfl_declare_varref(RV)

mbfl_math_expr_var mbfl_datavar(RV) '1.2 + 3.4'
$RV ⇒ 4.6

This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.