Menu

New Mathops module

Since there are several scenarios (most of them billing related) which
require floating point operations at script level, we've decided to put
together a new math operations module to handle all of them.

The *mathops *module handles floating point numbers represented as
strings at script level. Its functionalities are grouped in two categories:
-> rounding operations (trunc, ceil, floor, rounding to integers,
rounding to certain number of
decimal places, rounding to significant figures)
-> an expression evaluator (currently supports binary + - / *
operators and nested parantheses)

Quick example of using the evaluator:
-----------------------------------
loadmodule "mathops.so"
...
$avp(download_speed) = "8.755";
$avp(chunk_size) = "650.104";
$avp(chunks) = 5;

if (!math_eval("$avp(chunk_size) / $avp(download_speed) * $avp(chunks)",
"$avp(time)")) {
xlog("ERROR in expression evaluation!\n");
}

xlog("Download time left: $avp(time) seconds\n");
...
-------------------------------------

You can control the precision of the results with the /decimal_digits/
module parameter. For details regarding the rounding operations, please
browse the module documentation [1] posted on the project website.

Suggestions and feedback are appreciated as always.

[1]: http://www.opensips.org/html/docs/modules/devel/mathops.html

Posted by Bogdan-Andrei Iancu 2013-03-08