all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Helmut Eller <eller.helmut@gmail.com>
To: Stefan Monnier <monnier@IRO.UMontreal.CA>
Cc: 5114@emacsbugs.donarmstrong.com
Subject: bug#5114: 23.1.50; (string-to-number (number-to-string most-positive-fixnum))
Date: Sat, 05 Dec 2009 13:36:41 +0100	[thread overview]
Message-ID: <m2zl5x4mxy.fsf@gmail.com> (raw)
In-Reply-To: <jwvskbr6asf.fsf-monnier+emacsbugreports@gnu.org> (Stefan Monnier's message of "Thu, 03 Dec 2009 15:52:16 -0500")

[-- Attachment #1: Type: text/plain, Size: 614 bytes --]

* 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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: string-to-number.patch --]
[-- Type: text/x-diff, Size: 962 bytes --]

--- 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);
+	  }
+      }
+  }
 }
 
 \f

[-- Attachment #3: Type: text/plain, Size: 8 bytes --]


Helmut

  reply	other threads:[~2009-12-05 12:36 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-03 14:36 bug#5114: 23.1.50; (string-to-number (number-to-string most-positive-fixnum)) Helmut Eller
2009-12-03 20:52 ` Stefan Monnier
2009-12-05 12:36   ` Helmut Eller [this message]
2009-12-05 14:25     ` Eli Zaretskii
2009-12-05 15:18       ` Helmut Eller
2009-12-05 16:44         ` Eli Zaretskii
2011-09-18  9:47 ` Lars Magne Ingebrigtsen
2011-09-18 10:57   ` Leo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2zl5x4mxy.fsf@gmail.com \
    --to=eller.helmut@gmail.com \
    --cc=5114@emacsbugs.donarmstrong.com \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.