Re: "cannot coerce inexact literal to fixnum"

Peter Bex <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <ZcdXTcKUrOuXLQEb@doggett>
On Sat, Feb 10, 2024 at 08:12:38AM +0200, Al wrote:
> On 2024-02-10 02:42, Al wrote:
> 
> > ... if I enable fixnum, csc chokes on both the third and fourth
> > display's with: "Error: cannot coerce inexact literal `2147483647' to
> > fixnum". It compiles and runs fine if those lines are commented out (or
> > if fixnum is disabled).
> 
> So the error comes from a check for (big-fixnum?) in chicken-core/core.scm.
> It is defined in chicken-core/support.scm:
> 
>  (define (big-fixnum? x) ;; XXX: This should probably be in c-platform
>     (and (fixnum? x)
>          (feature? #:64bit)
>          (or (fx> x 1073741823)
>          (fx< x -1073741824) ) ) )
> 
>   (define (small-bignum? x) ;; XXX: This should probably be in c-platform
>     (and (bignum? x)
>          (not (feature? #:64bit))
>          (fx<= (integer-length x) 62) ) )
> 
> Maybe the condition in big-fixnum should be negated? Apply the restrictive
> #x3fffffff limit only when NOT (feature? #:64bit) ?

No, this code is correct.  It emits a literal into the C code.  That C code
must be able to be compiled on either 32-bit platforms or 64-bit platforms.

That's why on 64-bit platforms, we can't simply assume that a fixnum is
small enough to fit in a machine word - if the same code will be
compiled on a 32-bit machine it wouldn't fit in a fixnum.  These
so-called "big-fixnums" are compiled into a string literal which gets
decoded on-the-fly at runtime into either a fixnum (on 64-bit) or a
bignum (on 32-bit).

So you see, this wouldn't work with (declare fixnum) mode because that
must be able to assume fixnums throughout, regardless of target platform.

There's (currently) no option to force fixnum mode in a way that ignores
the existence 32-bit platforms.  Theoretically, it should be possible to
compile your code assuming fixnums (so it emits C integer literals) and
make it barf at compilation time if one tried to build for a 32-bit
platform using a #ifdef or something.  We just don't have the required
code to do this, and I'm not sure this is something we'd all want.

Cheers,
Peter
signature.asc (application/pgp-signature, 488 B)
-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEE6bh5EUSYP1EE+1UTER25+ctFibAFAmXHV0gACgkQER25+ctF
ibAU/wf+Jhfg8ozs4E/r6JRQ5dc6eKeIipVVSXbIA5sfNhjax2N1WsjtGKkTrf3R
9TZfT20ulv+5rV5gDTbAFlj8MmzI2JWow+W/vzUvy6NEEcb6sgRuno++dHgaE0Z+
pH7oFqoIM3SATtyqTFvuJleUXmlGErPK1mcNwVdmngZyDPRvQIR+2k9dUcrfnoad
E2Dmjh8wXQ1QjGno+FGQ84O+PP6Vio9B4AXwUoy95EcfY/DiaNMX6C2NHySXAyXs
v6qG/6ZV1Bs+Rqlneud5apE8maAO1ltYHyA7Anto47JxmSbeeIpt7oXMfvP8LAN0
Gf+7AXZClkVfE0UC7cA28ZBkh/c5kw==
=5nS0
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.