Skip to content

Commit

Permalink
math: added math_sqrt() function
Browse files Browse the repository at this point in the history
  • Loading branch information
miconda committed Apr 14, 2023
1 parent bb3c781 commit 388a12b
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/modules/math/math_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static int w_math_pow(sip_msg_t *msg, char *v1, char *v2, char *r);
static int w_math_logN(sip_msg_t *msg, char *v1, char *r);
static int w_math_log2(sip_msg_t *msg, char *v1, char *r);
static int w_math_log10(sip_msg_t *msg, char *v1, char *r);
static int w_math_sqrt(sip_msg_t *msg, char *v1, char *r);
static int fixup_math_p2(void **param, int param_no);
static int fixup_math_p3(void **param, int param_no);

Expand All @@ -53,6 +54,8 @@ static cmd_export_t cmds[]={
0, ANY_ROUTE},
{"math_log10", (cmd_function)w_math_log10, 2, fixup_math_p2,
0, ANY_ROUTE},
{"math_sqrt", (cmd_function)w_math_sqrt, 2, fixup_math_p2,
0, ANY_ROUTE},

{0, 0, 0, 0, 0, 0}
};
Expand Down Expand Up @@ -189,6 +192,34 @@ static int w_math_log10(sip_msg_t *msg, char *v1, char *r)
return 1;
}

/**
*
*/
static int w_math_sqrt(sip_msg_t *msg, char *v1, char *r)
{
int vi1 = 0;
pv_spec_t *dst;
pv_value_t val = {0};

if(fixup_get_ivalue(msg, (gparam_t*)v1, &vi1)<0) {
LM_ERR("failed to get first parameter value\n");
return -1;
}

dst = (pv_spec_t *)r;
if(dst->setf==NULL) {
LM_ERR("target pv is not writable\n");
return -1;
}

val.ri = (long)sqrt((double)vi1);
val.flags = PV_TYPE_INT|PV_VAL_INT;

dst->setf(msg, &dst->pvp, (int)EQ_T, &val);

return 1;
}

/**
*
*/
Expand Down

0 comments on commit 388a12b

Please sign in to comment.