[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Inconsistency: sometimes an integer, sometimes a float
From: |
Pascal J. Bourguignon |
Subject: |
Re: Inconsistency: sometimes an integer, sometimes a float |
Date: |
Fri, 24 Jan 2014 20:28:32 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
Doug Lewan <dougl@shubertticketing.com> writes:
> Cool bug! How in the world did you find it?
It is not a bug, it is a design of the language. Yes, emacs lisp is
ill-designed. If you want a better designed lisp, consider Common Lisp.
Namely, emacs numeric datatypes are limited to fixnums and floats.
see: most-positive-fixnum
On 32-bit systems, fixnums are limited to 29 bits.
most-positive-fixnum --> 536870911
(format "%o " 536870911) --> "3777777777"
(integer-length 536870911) --> 29
On 64-bit systems, fixnums are limited to 61 bits.
most-positive-fixnum --> 2305843009213693951
(/ 2305843009213693951 1000000) --> 2305843009213
(/ (1+ 2305843009213693951) 1000000) --> -2305843009213
(/ 2305843009213693952 1000000) --> 2305843009213.694
In emacs lisp, / is very wrong, since (/ 2 3) --> 0
For floating point numbers it is ok: (/ 2.0 3.0) --> 0.6666666666666666
but of course, people often have very wrong expectations about floating
point numbers, I can't fathom why.
Notice that 1+ (like +, -, *, etc) is also very wrong, on emacs lisp
fixnums, since (minusp (1+ most-positive-fixnum)).
On the other hand, reading 536870912 on a 32-bit emacs, or
2305843009213693952 on a 64-bit emacs, reads a floating point number
(since there is no such fixnum).
This is clearly ludicrously insane!
If you want to compute, better use Common Lisp:
cl-user> (/ most-positive-fixnum 1000000)
46116860184273879/40000
cl-user> (/ (1+ most-positive-fixnum) 1000000)
18014398509481984/15625
cl-user> (/ (float (1+ most-positive-fixnum)) 1000000)
1.1529215E+12
cl-user> (/ (complex most-positive-fixnum most-positive-double-float) 1000000)
#C(1.152921504606847D+12 1.797693134862316D+302)
--
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ? C'est le moment d'acheter !"
- Inconsistency: sometimes an integer, sometimes a float, djc, 2014/01/23
- Re: Inconsistency: sometimes an integer, sometimes a float, Rasmus, 2014/01/23
- Re: Inconsistency: sometimes an integer, sometimes a float, Nicolas Richard, 2014/01/23
- Re: Inconsistency: sometimes an integer, sometimes a float, Nicolas Richard, 2014/01/23
- Re: Inconsistency: sometimes an integer, sometimes a float, Eli Zaretskii, 2014/01/23
- Re: Inconsistency: sometimes an integer, sometimes a float, Pascal J. Bourguignon, 2014/01/23
- RE: Inconsistency: sometimes an integer, sometimes a float, Doug Lewan, 2014/01/24
- Message not available
- Re: Inconsistency: sometimes an integer, sometimes a float,
Pascal J. Bourguignon <=
- Message not available