num2expr is a header-only C library to convert a floating-point number into some specific mathematical expressions.
A very fast guide:
#define NUM2EXPR_IMPLEMENTATION
#define NUM2EXPR_NEED_PRINT_FUNCTION
#include "num2expr.h"
int main(void) {
num2expr_expr* expr = num2expr(2 * M_PI / 3);
// will print "((2*pi)/3)"
num2expr_print_expr(expr);
printf("\n");
num2expr_free_expr(expr);
return 0;
}It supports the following formats:
$\frac{a}{b}$ $\frac{\sqrt{a}}{b}$ $\frac{\sqrt{a}+\sqrt{b}}{c}$ $\text{some value}\times\pi$
We use a new algorithm to calculate the (3) format, You can find more information about the algorithm in this blog.
All codes were also written in Python (but with no expression tree).