Subject: Re: bug#5114: 23.1.50; (string-to-number (number-to-string most-positive-fixnum))

From: Helmut Eller <[email-masked]>
Date: Sat, 05 Dec 2009 13:36:41 +0100

* Stefan Monnier [2009-12-03 21:52+0100] writes:

>> (setq x (string-to-number (number-to-string most-positive-fixnum)))
>> (= most-positive-fixnum x) => nil
>    
>> x is 2305843009213693440 but it should be most-positive-fixnum
>> which is 2305843009213693951.
>
>> The test
>> (= most-positive-fixnum 
>>    (string-to-number (number-to-string most-positive-fixnum)))
>> seems to work as expected on 32-bit machines but not so on 64 bit.
>
> Indeed, it passes through a floating point conversion, so there's only
> abour 52 bit of precesion.

Here is a patch for string-to-number to use the full fixnum range:

--- data.c.~1.308.~	2009-12-05 08:07:48.000000000 +0100
+++ data.c	2009-12-05 13:27:33.000000000 +0100
@@ -2393,23 +2393,26 @@
     p++;
 
   if (isfloat_string (p, 1) && b == 10)
-    val = make_float (sign * atof (p));
-  else
-    {
-      double v = 0;
-
-      while (1)
-	{
-	  int digit = digit_to_number (*p++, b);
-	  if (digit < 0)
-	    break;
-	  v = v * b + digit;
-	}
-
-      val = make_fixnum_or_float (sign * v);
-    }
-
-  return val;
+    return make_float (sign * atof (p));
+  else {
+    unsigned long u = 0;
+    while (1)
+      {
+	int digit = digit_to_number (*p++, b);
+	if (digit < 0)
+	  return make_number (sign * u);
+	else if (u <= (MOST_POSITIVE_FIXNUM - digit) / b)
+	  u = u * b + digit;
+	else
+	  {
+	    /* overflow to flonums */
+	    double f = ((double)u) * b + digit;
+	    while (digit = digit_to_number (*p++, b), digit >= 0)
+	      f = f * b + digit;
+	    return make_float (sign * f);
+	  }
+      }
+  }
 }
 
 

Helmut