GNU bug report logs - #5114
23.1.50; (string-to-number (number-to-string most-positive-fixnum))

Previous Next

Package: emacs;

Reported by: Helmut Eller <eller.helmut <at> gmail.com>

Date: Thu, 3 Dec 2009 15:05:07 UTC

Severity: normal

Tags: fixed

Fixed in version 24.1

Done: Lars Magne Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

Full log


Message #15 received at 5114 <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Helmut Eller <eller.helmut <at> gmail.com>
To: Stefan Monnier <monnier <at> IRO.UMontreal.CA>
Cc: 5114 <at> debbugs.gnu.org
Subject: Re: bug#5114: 23.1.50; (string-to-number (number-to-string most-positive-fixnum))
Date: Sat, 05 Dec 2009 13:36:41 +0100
[Message part 1 (text/plain, inline)]
* 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:

[string-to-number.patch (text/x-diff, inline)]
--- 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);
+	  }
+      }
+  }
 }
 
 
[Message part 3 (text/plain, inline)]
Helmut

This bug report was last modified 13 years and 245 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.