[Chicken-users] Value returned from modulo

2010-08-28 Thread Jeronimo Pellegrini
Hello,

I was writing a simple PRNG for didatic purposes and found
that in Chicken, modulo may or may not return an exact
integer -- whic is OK, I can just do inexact-exact.
But it seems also that the value varies among systems:

#;1 (define a 1103515245)
#;2 (define b 12345)
#;3 (define m (expt 2 32))
#;4 (define x 631629065)
#;5 (modulo (+ (* a x) b) m)
23163648.0
#;6 (+ (* a x) b)
697012302412608270

Octave also gives me 23163648:
octave:2 mod((1103515245*631629065 + 12345),(2^32))
ans =  23163648

From Gambit, and SBCL I get 23163662.

Gambit v4.6.0
 (define a 1103515245)
 (define b 12345)
 (define m (expt 2 32))
 (define x 631629065)
 (modulo (+ (* a x) b) m)
23163662
 (+ (* a x) b)
697012302412608270

(Same for Chibi, Gauche and Bigloo)


SBCL:

* (setq a 1103515245)
* (setq b 12345)
* (setq m (expt 2 32))
* (setq x 631629065)
* (mod  (+ (* a x) b) m)

23163662
* (+ (* a x) b)

697012302412608270

Maxima also answers 23163662 and 697012302412608270.

What puzzles me is that the large value (before taking the modulo)
is 697012302412608270 in all systems, but Chiken and Octave will
still return 23163648 from modulo, so it's not that the larger value
was not represented exactly. Why then is 23163648 returned?

I'm not using the numbers egg (I'd like students to do as much as
possible from scratch, using standard Scheme)

I'm using Chicken from git, master branch, compiled oon a 64-bit
machine, so I suppose I can represent all these numbers without
problems:

CHICKEN
(c)2008-2010 The Chicken Team
(c)2000-2007 Felix L. Winkelmann
Version 4.6.0rc1 
linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
compiled 2010-08-09 on newton (Linux)

J.


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


Fwd: [Chicken-users] Value returned from modulo

2010-08-28 Thread Phil Bewig
You can add Chez Scheme to the list of those that gets the correct answer
23163662.

Perhaps (+ (* a x) b) is calculated exactly but the modulo operator uses
inexact arithmetic internally; you mentioned that the returned value was
inexact.  When I do (modulo (+ (* (exact-inexact a) (exact-inexact x))
(exact-inexact b)) (expt 2.0 32) I get 23163648.0.

On Sat, Aug 28, 2010 at 6:19 AM, Jeronimo Pellegrini j...@aleph0.infowrote:

 Hello,

 I was writing a simple PRNG for didatic purposes and found
 that in Chicken, modulo may or may not return an exact
 integer -- whic is OK, I can just do inexact-exact.
 But it seems also that the value varies among systems:

 #;1 (define a 1103515245)
 #;2 (define b 12345)
 #;3 (define m (expt 2 32))
 #;4 (define x 631629065)
 #;5 (modulo (+ (* a x) b) m)
 23163648.0
 #;6 (+ (* a x) b)
 697012302412608270

 Octave also gives me 23163648:
 octave:2 mod((1103515245*631629065 + 12345),(2^32))
 ans =  23163648

 From Gambit, and SBCL I get 23163662.

 Gambit v4.6.0
  (define a 1103515245)
  (define b 12345)
  (define m (expt 2 32))
  (define x 631629065)
  (modulo (+ (* a x) b) m)
 23163662
  (+ (* a x) b)
 697012302412608270

 (Same for Chibi, Gauche and Bigloo)


 SBCL:

 * (setq a 1103515245)
 * (setq b 12345)
 * (setq m (expt 2 32))
 * (setq x 631629065)
 * (mod  (+ (* a x) b) m)

 23163662
 * (+ (* a x) b)

 697012302412608270

 Maxima also answers 23163662 and 697012302412608270.

 What puzzles me is that the large value (before taking the modulo)
 is 697012302412608270 in all systems, but Chiken and Octave will
 still return 23163648 from modulo, so it's not that the larger value
 was not represented exactly. Why then is 23163648 returned?

 I'm not using the numbers egg (I'd like students to do as much as
 possible from scratch, using standard Scheme)

 I'm using Chicken from git, master branch, compiled oon a 64-bit
 machine, so I suppose I can represent all these numbers without
 problems:

 CHICKEN
 (c)2008-2010 The Chicken Team
 (c)2000-2007 Felix L. Winkelmann
 Version 4.6.0rc1
 linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
 compiled 2010-08-09 on newton (Linux)

 J.


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

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