Arc Forumnew | comments | leaders | submitlogin
Problem with factorial and rainbow
2 points by jsgrahamus 2511 days ago | 5 comments
C:\Users\Steve\Documents\Programming\Lisp\arc\rainbow\src\arc>java -jar -server rainbow.jar

* redefining no

* redefining map1

* redefining pr

* redefining list

repl in 7045ms

arc> (def factorial (n) (if (> n 0) (* n (factorial (- n 1))) 1))

(fn (n) (if (> n 0) (* n (factorial (- n 1))) 1))

arc> (factorial 5)

120

arc> (factorial 50)

-3258495067890909184

arc>

Why does this differ from arc3.1?

Thanks, Steve



3 points by zck 2511 days ago | link

Does rainbow support bignums? I can't find anything about it on rainbow's site: https://github.com/conanite/rainbow#readme

Check values between (factorial 5) and (factorial 50). You'll probably hit MAXINT and wrap into negative.

-----

2 points by rocketnia 2510 days ago | link

Yeah, I guess Rainbow doesn't bother with bignums. Jarc has exactly the same result, probably because they both use JVM longs. They both still let us use JVM bignums the long way though:

  ; This code will work on both Rainbow and Jarc.
  (when (errsafe jarc.Jarc.class) use!rainbow)
  
  (def big (x)
    (java-static-invoke "java.math.BigInteger" 'valueOf x))
  
  (def bigfact (x)
    (if (< x 0)  nil
        (< 0 x)  ((bigfact:- x 1) 'multiply big.x)
                 big.1))

  Rainbow:
  
  arc> (= foo bigfact.50)
  30414093201713378043612608166064768844377641568960512000000000000
  arc> type.foo
  java-object
  arc> foo!getClass
  class java.math.BigInteger
  
  
  Jarc:
  
  Jarc> (= foo bigfact.50)
  #java.math.BigInteger(743210128)
  Jarc> foo!toString
  "30414093201713378043612608166064768844377641568960512000000000000"
  Jarc> type.foo
  java.math.BigInteger
  Jarc> foo!getClass
  #class(java.math.BigInteger)

-----

1 point by rocketnia 2510 days ago | link

Hmm, Semi-Arc does have bignums. ^_^

  arc> (= foo 1)
  1
  arc> (for i 1 50 (zap * foo i))
  nil
  arc> foo
  30414093201713378043612608166064768844377641568960512000000000000
  arc> type.foo
  int
  arc> (/ 1 foo)
  1/30414093201713378043612608166064768844377641568960512000000000000

-----

2 points by waterhouse 2510 days ago | link

  (def likely-maxint ()
    ((afn (n)
       (if (< n 0)
           (- n 1)
           (self (* n 2))))
      1))
Funsies. (Will loop infinitely on implementations that use bignums.)

-----

1 point by rocketnia 2510 days ago | link

That just so happens to yield java.lang.Long.MAX_VALUE on both Jarc and Rainbow. ^_^

-----