[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Checking for loss of information on integer conversion
From: |
Paul Eggert |
Subject: |
Checking for loss of information on integer conversion |
Date: |
Sat, 17 Feb 2018 17:27:37 -0800 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 |
Bug#30408 reminded me of a problem that's bitten me before, which is that Emacs
Lisp reading and printing sometimes loses information when converting from
integers to strings or vice versa, and this loss of information can lead to real
problems. Without going to bignums we can't fix this problem nicely in general;
however, Emacs can do a better job of signaling an error when information is
lost in the current implementation. I've proposed a patch here:
https://debbugs.gnu.org/30408#16
and am attaching it to this email for convenience.
Briefly, the patch is twofold. First, it causes calls like (format "%d"
18446744073709551616) to return the mathematically-correct value
"18446744073709551616" instead of silently returning the
mathematically-incorrect value "9223372036854775807" as they do now. If the
function cannot return the correct value due to an implementation limit -- e.g.,
(format "%x" 18446744073709551616) -- the patch causes the function to signal an
overflow.
Second, although Emacs still reads large integers like 18446744073709551616 as
if they were floating-point, it now signals an error if information is lost in
the process. For example, the number 18446744073709551615 now causes the reader
to signal an error, since it cannot be represented exactly either as a fixnum or
as a floating-point number. If you want inexact representation, you can append
".0" or "e0" to the integer.
As these are incompatible changes to Emacs I thought I'd mention them on this
list.
Another possibility would be for Emacs to signal an error when reading any
integer that does not fit in fixnum bounds. That would be a bigger change, though.
0001-Avoid-losing-info-when-converting-integers.patch
Description: Text Data
- Checking for loss of information on integer conversion,
Paul Eggert <=