Jump to content
Eternal Lands Official Forums
jaceks

143 % 8 = 7

Recommended Posts

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
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! :pickaxe:

Share this post


Link to post
Share on other sites
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 by jaceks

Share this post


Link to post
Share on other sites

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 by bluap

Share this post


Link to post
Share on other sites
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
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. :D

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
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. :P

Hehe, yes, thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×