sqrt bignum bug

From: Ron Stanonik <stanonik_at_Cogsci.ucsd.edu>
Date: Thu, 21 Mar 2002 12:52:57 -0800 (PST)

When the argument to sqrt is a bignum and not a perfect square,
the result is a flonum, but only the integer part of the root.
This happens in STk-4.0.1-1. We ran into this with code similar to

  (trace sqrt)
  (sqrt (random 100))

For example, if (random 100) returns 83, then sqrt returns 9.0.

Here's a diff of our fix

--- /tmp/number.c.orig Wed Mar 20 11:59:02 2002
+++ Src/number.c Wed Mar 20 11:59:11 2002
_at_@ -1157,7 +1157,7 @@ PRIMITIVE STk_sqrt(SCM z)
         }
         else {
           /* Result is a flonum */
- res = STk_makenumber(bignum2double(&root));
+ res = STk_makenumber(sqrt(bignum2double(BIGNUM(z))));
         }
         mpz_clear(&root); mpz_clear(&remainder);
         return res;


It's interesting, but probably doesn't qualify as a bug, that (random 100)
returns a bignum and not an integer. random uses rand, which returns a
32 bit number, which becomes a bignum. The modulo computation is also
performed assuming a bignum, so the result is bignum, even if the value
isn't a bignum.

Ron Stanonik
stanonik_at_cogsci.ucsd.edu
Received on Thu Mar 21 2002 - 21:52:58 CET

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