jaceks Report post Posted February 7, 2009 Hi, IMHO #calc command lacks modulo operator, so I did attempt to enhance it a bit. I tested new functionality at test server and it works well. example of syntax: #calc 143 % 8 patch as follow: 23a24 > #define CALCTOK_MOD 11 184a186,197 > //modulo > if(t1==CALCTOK_NUM&&t2==CALCTOK_MOD&&t3==CALCTOK_NUM){ > calcpop(cs);calcpop(cs);calcpop(cs); > nt=(CalcTok*)malloc(sizeof(CalcTok)); > if(cs1->value!=0){ > nt->type=CALCTOK_NUM; > nt->value=cs3->value-(((int)(cs3->value/cs1->value))*cs1->value); > } else calc_error=CALCERR_DIVIDE; > calcpush(cs,nt); > free(cs1);free(cs2);free(cs3); > return 1; > } 234a248,250 > case '%': > ct->type=CALCTOK_MOD;pos++; > break; please consider to use it in official client jaceks Share this post Link to post Share on other sites
bluap Report post Posted February 7, 2009 IMHO #calc command lacks modulo operator, so I did attempt to enhance it a bit.I tested new functionality at test server and it works well. example of syntax: #calc 143 % 8 patch as follow: ... please consider to use it in official client This patch looks fine to me. I presume you have calculated the actual modulus the hard way so non-whole numbers can be used. If no one objects, I commit this to the client. Your patch keeps the style of the surrounding code which is great but you used spaces for indent rather than the prefered and existing hard tabs. Also, for more complex patches at least, the prefered format for diff uses the "-Nau[r]" options. If you haven't done so already, have a read of the sticky posts in this section for more information. Thanks for this patch! Share this post Link to post Share on other sites
jaceks Report post Posted February 7, 2009 (edited) I presume you have calculated the actual modulus the hard way so non-whole numbers can be used. thanks bluap, I didn't consider that arguments can be non-whole numbers (modulo operation is useful for cargo capacity calculation), but you are right, things shall be done well; a bit modified patch follow: 23a24 > #define CALCTOK_MOD 11 184a186,197 > //modulo > if(t1==CALCTOK_NUM&&t2==CALCTOK_MOD&&t3==CALCTOK_NUM){ > calcpop(cs);calcpop(cs);calcpop(cs); > nt=(CalcTok*)malloc(sizeof(CalcTok)); > if(cs1->value!=0){ > nt->type=CALCTOK_NUM; > nt->value=(int)(cs3->value-(((int)(cs3->value/cs1->value))*cs1->value)); > } else calc_error=CALCERR_DIVIDE; > calcpush(cs,nt); > free(cs1);free(cs2);free(cs3); > return 1; > } 234a248,250 > case '%': > ct->type=CALCTOK_MOD;pos++; > break; retested at test server and it works well diff in requested format: --- calc.c 2009-02-07 11:37:45.000000000 +0100 +++ ../elc-my/calc.c 2009-02-07 17:15:31.000000000 +0100 @@ -21,6 +21,7 @@ #define CALCTOK_END 8 #define CALCTOK_XOP 9 #define CALCTOK_LOP 10 +#define CALCTOK_MOD 11 /*Implementation of #calc command @@ -182,6 +183,18 @@ free(cs2);free(cs3);free(cs4); return 1; } + //modulo + if(t1==CALCTOK_NUM&&t2==CALCTOK_MOD&&t3==CALCTOK_NUM){ + calcpop(cs);calcpop(cs);calcpop(cs); + nt=(CalcTok*)malloc(sizeof(CalcTok)); + if(cs1->value!=0){ + nt->type=CALCTOK_NUM; + nt->value=(int)(cs3->value-(((int)(cs3->value/cs1->value))*cs1->value)); + } else calc_error=CALCERR_DIVIDE; + calcpush(cs,nt); + free(cs1);free(cs2);free(cs3); + return 1; + } //pars if(t1==CALCTOK_CPAR&&t2==CALCTOK_NUM&&t3==CALCTOK_OPAR){ calcpop(cs);calcpop(cs);calcpop(cs); @@ -232,6 +245,9 @@ case '/': ct->type=CALCTOK_DIV;pos++; break; + case '%': + ct->type=CALCTOK_MOD;pos++; + break; case '0': case '1': case '2': jaceks Edited February 7, 2009 by jaceks Share this post Link to post Share on other sites
bluap Report post Posted February 7, 2009 (edited) Sorry, I didn't make my point too well. I was happy with the calculation working for non-whole numbers, it might be useful. My point was that if you only wanted to consider whole numbers, you could have used: nt->value=(int)cs3->value%(int)cs1->value; Edited February 7, 2009 by bluap Share this post Link to post Share on other sites
jaceks Report post Posted February 7, 2009 Sorry, I didn't make my point too well. I was happy with the calculation working for non-whole numbers, it might be useful. My point was that if you only wanted to consider whole numbers, you could have used: --- calc.c 2009-02-07 11:37:45.000000000 +0100 +++ ../elc-my/calc.c 2009-02-07 21:40:07.000000000 +0100 @@ -21,6 +21,7 @@ #define CALCTOK_END 8 #define CALCTOK_XOP 9 #define CALCTOK_LOP 10 +#define CALCTOK_MOD 11 /*Implementation of #calc command @@ -182,6 +183,18 @@ free(cs2);free(cs3);free(cs4); return 1; } + //modulo + if(t1==CALCTOK_NUM&&t2==CALCTOK_MOD&&t3==CALCTOK_NUM){ + calcpop(cs);calcpop(cs);calcpop(cs); + nt=(CalcTok*)malloc(sizeof(CalcTok)); + if(cs1->value!=0){ + nt->type=CALCTOK_NUM; + nt->value=(int)cs3->value%(int)cs1->value; + } else calc_error=CALCERR_DIVIDE; + calcpush(cs,nt); + free(cs1);free(cs2);free(cs3); + return 1; + } //pars if(t1==CALCTOK_CPAR&&t2==CALCTOK_NUM&&t3==CALCTOK_OPAR){ calcpop(cs);calcpop(cs);calcpop(cs); @@ -232,6 +245,9 @@ case '/': ct->type=CALCTOK_DIV;pos++; break; + case '%': + ct->type=CALCTOK_MOD;pos++; + break; case '0': case '1': case '2': Share this post Link to post Share on other sites
Vegar Report post Posted February 12, 2009 Why don't you just use modf() from math.h? double modf(double x, double *iptr); Share this post Link to post Share on other sites
bluap Report post Posted February 13, 2009 Why don't you just use modf() from math.h? double modf(double x, double *iptr); I presume you meant fmod(double x, double y). That is better than my casting suggestion which would crash with "#calc 1 % 0.1" for example. I've now commited this patch to CVS using fmod(). Thanks jaceks for the patch. Share this post Link to post Share on other sites
Vegar Report post Posted February 14, 2009 I presume you meant fmod(double x, double y). That is better than my casting suggestion which would crash with "#calc 1 % 0.1" for example. Hehe, yes, thanks. Share this post Link to post Share on other sites