Re: stk arithmetic bug

From: Erick Gallesio <eg_at_kaolin.unice.fr>
Date: Fri, 09 Jan 1998 09:00:35 +0100

Daniel Boley writes:
> There is a serious flaw in the STK integer arithmetic, as illustrated below
> The same happens in STK 3.1 and also on an SGI machine.
>
> - Dan Boley
>
> Welcome to the STk interpreter version 3.1.1 [SunOS-5.5-sun4]
> Copyright © 1993-1996 Erick Gallesio - I3S - CNRS / ESSI <eg_at_unice.fr>
> STk> (* 10000 -1000) ;; -> WRONG ANSWER
> 42949662960000
> STk> (* -1000 10000) ;; -> RIGHT ANSWER
> -10000000

Ooops, funny that this problem was not discovered before. As Intel would says
this could not arrive before a million of years :). In fact, this is quite
easy to produce: when you multiply two small integers (the second one being
negative) and the result is bigger than a small integer (a mall int being
less than 2^24 for a 32 bits machine). Here is a patch for this bug.

--------------------------------------------------
diff -c -r1.11 number.c
*** number.c 1997/12/15 12:55:29 1.11
--- number.c 1998/01/07 17:44:58
***************
*** 393,400 ****
                  if (SMALLNUMP(prod=(double) INTEGER(*x) * INTEGER(y)))
                    SET_INTEGER(*x, (long) prod);
                  else {
                    *x = int2bignum(INTEGER(*x));
! mpz_mul_ui(BIGNUM(*x), BIGNUM(*x), INTEGER(y));
                  }
                }
                break;
--- 393,404 ----
                  if (SMALLNUMP(prod=(double) INTEGER(*x) * INTEGER(y)))
                    SET_INTEGER(*x, (long) prod);
                  else {
+ MP_INT tmp;
+
                    *x = int2bignum(INTEGER(*x));
! mpz_init_set_si(&tmp, INTEGER(y));
! mpz_mul(BIGNUM(*x), BIGNUM(*x), &tmp);
! mpz_clear(&tmp);
                  }
                }
                break;
-----------------------------------------------------

                -- Erick
Received on Fri Jan 09 1998 - 09:26:55 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST