dc: make 4 % 0 emit error messgaes and set result to 0
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 16 May 2019 07:40:36 +0000 (09:40 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 16 May 2019 07:40:36 +0000 (09:40 +0200)
function                                             old     new   delta
mod                                                  105     136     +31

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/dc.c

index 5119c13836e824023092cf035408c702ee973a45..5aef64b602fa3cb16eef6f4384b159760b074b51 100644 (file)
@@ -94,13 +94,18 @@ static void mod(void)
 {
        data_t d = pop();
 
-       //if (d == 0) {
-       //      bb_error_msg("remainder by zero");
-       //      pop();
-       //      push(0);
-       //      return;
-       //}
-       //^^^^ without this, we simply get SIGFPE and die
+       /* compat with dc (GNU bc 1.07.1) 1.4.1:
+        * $ dc -e '4 0 % p'
+        * dc: remainder by zero
+        * 0
+        */
+       if (d == 0) {
+               bb_error_msg("remainder by zero");
+               pop();
+               push(0);
+               return;
+       }
+       /* ^^^^ without this, we simply get SIGFPE and die */
 
        push((data_t) pop() % d);
 }