libqalculate-5.8.2

Index

# Introduction

libqalculate is a math library for expression evaluation with units, variables and functions support and CAS functionality.

The main parts of the library is the almighty Calculator class, the MathStructure class for mathematical expressions and classes for objects in an expression, mostly of the class Numbers and sub classes of ExpressionItem.

A simple application using libqalculate need only create a calculator object, perhaps load definitions (functions, variables, units, etc.), and calculate (and output) an expression as follows:

new Calculator();
CALCULATOR->loadExchangeRates();
CALCULATOR->loadGlobalDefinitions();
CALCULATOR->loadLocalDefinitions();
cout << CALCULATOR->calculateAndPrint("1 + 1", 2000) << endl;
Calculator
The almighty calculator class.
Definition Calculator.h:270

In the above example, the calculation is terminated after two seconds (2000 ms), if it is not finished before then. Applications using localized numbers should first call Calculator::unlocalizeExpression() on the expression.

A less simple application might calculate and output the expression separately.

EvaluationOptions eo;
MathStructure result;
CALCULATOR->calculate(&mstruct, unlocalizeExpression("1 + 1"), 2000, eo);
MathStructure
A structure representing a mathematical value/expression/result.
Definition MathStructure.h:150
EvaluationOptions
Options for calculation.
Definition includes.h:748

More complex usage mainly involves manipulating objects of the MathStructure class directly.

To display the resulting expression use Calculator::print() as follows:

PrintOptions po;
string result_str = CALCULATOR->print(result, 2000, po);
PrintOptions
Options for formatting and display of mathematical structures/results.
Definition includes.h:466

Alternatively MathStructure::format() followed by MathStructure::print() can be used, without the possibility to specify a time limit.

Central to the flexibility of libqalculate is the many options passed to evaluating and display functions with EvaluationOptions and PrintOptions.

# Using the library

libqalculate uses pkg-config.

For a simple program use pkg-config on the command line:

c++ `pkg-config --cflags --libs libqalculate` hello.c -o hello

If the program uses autoconf, put the following in configure.ac:

PKG_CHECK_MODULES(QALCULATE, [
libqalculate >= 1.0.0
])
AC_SUBST(QALCULATE_CFLAGS)
AC_SUBST(QALCULATE_LIBS)