Hi Gergely, Thanks for the patch, I see an immediate use-case for this. I applied the patch to the 3.4 tree, and at the same time, I've changed it slightly. On Tue, 2011-05-03 at 22:19 +0200, Gergely Nagy wrote:
Implement addition, substraction, multiplication, division and modulus template functions: $(+ N M), $(- N M), $(* N M), $(/ N M) and $(% N M), respectively.
All of them take two numeric arguments, and log an error,
instead of logging an error, always, insert a "NaN" into the output stream, which stands for Not-a-Number, commonly used to represent non-numbers in underflows. I know it's not that intuitive, but in production environments, generating loads of error messages such as these in the fast path (even more, since you can several invocation of these functions per message), would do no good. So, I've changed errors to debug (so if someone is running it with debugging enabled still get the messages), and in all error paths I've used NaN instead of the empty string. Also, tests didn't cover negative numbers, now I've added to cover them too. Also, while I was at it, I've changed all g_string_append_printf() calls to format_int32_padded(), which is much faster. g_string_append_printf() is double slow, because: 1) it uses printf, but to measure the length of the string, it runs printf without storing its results, and then 2) allocates a buffer to format this result, and then 3) moves the temp buffer to the end of the gstring 4) frees the buffer. So it is not even double, but quadraple slow. :)
if they receive less or more, or in case the number cannot be fully parsed as a number.
Signed-off-by: Gergely Nagy <algernon@balabit.hu>
This is the commit message, please let me know if you disagree with my changes. commit 89b5006f1fc015f1049ec3713638074434beba65 Author: Balazs Scheidler <bazsi@balabit.hu> Date: Tue May 3 23:01:09 2011 +0200 basicfuncs: Implement a few numeric template functions Implement addition, substraction, multiplication, division and modulus template functions: $(+ N M), $(- N M), $(* N M), $(/ N M) and $(% N M), respectively. All of them take two numeric arguments, and result in "NaN" (aka Not-a-Number) in case parameters cannot be parsed, or if a division by zero would occur. Signed-off-by: Gergely Nagy <algernon@balabit.hu> Signed-off-by: Balazs Scheidler <bazsi@balabit.hu> -- Bazsi