Re: [Chicken-users] (abs 2147483648) evaluates to negative number

2009-07-31 Thread Jim Ursetto
On Fri, Jul 31, 2009 at 8:02 AM, Alejandro Forero
Cuervo wrote:
> In Chicken 3.4.0 linux-unix-gnu-x86-64 the expression (abs 2147483648)
> evaluates to -2147483648 in both csi and a compiled program (negative?
> and positive?, however, seem to work correctly).  It evaluated
> correctly in a 32-bits build of the same version.

Please try attached patch.
Jim
Index: runtime.c
===
--- runtime.c   (revision 15090)
+++ runtime.c   (working copy)
@@ -5382,7 +5382,7 @@
 
 C_regparm C_word C_fcall C_a_i_abs(C_word **a, int c, C_word x)
 {
-  if(x & C_FIXNUM_BIT) return C_fix(abs(C_unfix(x)));
+  if(x & C_FIXNUM_BIT) return C_fix(labs(C_unfix(x)));
 
   if(C_immediatep(x) || C_block_header(x) != C_FLONUM_TAG)
 barf(C_BAD_ARGUMENT_TYPE_ERROR, "abs", x);
___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] (abs 2147483648) evaluates to negative number

2009-07-31 Thread John Cowan
John Cowan scripsit:

> The bug is still present in 4.1.1, and it's drastic:
> 
> #;1> (abs 2147483648)
> -2147483648
> #;2> (abs 2147483649)
> 2147483647
> #;3> (abs 2147483650)
> 2147483646
> #;4> (abs 4294967296)
> 0

Oops, dropped the following:

Almost certainly this is happening because the fixnum is being converted
to a C int, which is only 32 bits even on 64-bit architectures, at least
the ones we support (in the standard LP64 model, ints are 32-bit and
only longs and pointers are 64-bit).

A fixnum should always be converted to a C long, which will retain
full 63-bit precision on 64-bit systems and still provide 31 bits on
32-bit systems.  It wouldn't surprise me if there were a fair number of
instances of this bug in various parts of Chicken's C code.

A question that remains is what to do when calling an existing C routine
that expects an int with a fixnum that is too big to fit in 32 bits.
Just truncating the fixnum doesn't seem like a Good Thing.

-- 
John Cowan   co...@ccil.org  http://www.ccil.org/~cowan
Most languages are dramatically underdescribed, and at least one is
dramatically overdescribed.  Still other languages are simultaneously
overdescribed and underdescribed.  Welsh pertains to the third category.
--Alan King


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] (abs 2147483648) evaluates to negative number

2009-07-31 Thread John Cowan
Alejandro Forero Cuervo scripsit:

> In Chicken 3.4.0 linux-unix-gnu-x86-64 the expression (abs 2147483648)
> evaluates to -2147483648 in both csi and a compiled program (negative?
> and positive?, however, seem to work correctly).

The bug is still present in 4.1.1, and it's drastic:

#;1> (abs 2147483648)
-2147483648
#;2> (abs 2147483649)
2147483647
#;3> (abs 2147483650)
2147483646
#;4> (abs 4294967296)
0

> It evaluated correctly in a 32-bits build of the same version.

It works in 32-bit Chicken because 2147483648 (like every integer greater
than 1073741823) is a flonum.

> In the meantime, I'll put the following in my programs.  Other people
> may want to do the same:
> 
>   (when (negative? (abs 2147483648))
> (set! abs
>   (lambda (x)
> (if (negative? x)
>   (- x)
>   x

I'd just override the definition of abs to begin with.

-- 
John Cowan  co...@ccil.org  http://www.ccil.org/~cowan
Any day you get all five woodpeckers is a good day.  --Elliotte Rusty Harold


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


[Chicken-users] (abs 2147483648) evaluates to negative number

2009-07-31 Thread Alejandro Forero Cuervo
In Chicken 3.4.0 linux-unix-gnu-x86-64 the expression (abs 2147483648)
evaluates to -2147483648 in both csi and a compiled program (negative?
and positive?, however, seem to work correctly).  It evaluated
correctly in a 32-bits build of the same version.

Is this a known issue?  Has it been fixed in a newer version?

Thanks!

Alejo.
http://azul.freaks-unidos.net/


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users