]> gitweb.factorcode.org Git - factor.git/commitdiff
math.factorials: making double-factorial work for negative numbers.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Apr 2013 04:44:35 +0000 (21:44 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Apr 2013 04:44:35 +0000 (21:44 -0700)
extra/math/factorials/factorials-tests.factor
extra/math/factorials/factorials.factor

index e40b20b9ddea42606a8ad0af9abdded195bd23aa..933646cda94799a5835afd024771b636f123e04c 100644 (file)
@@ -1,4 +1,4 @@
-USING: kernel math.functions sequences tools.test ;
+USING: kernel math.functions math.ranges sequences tools.test ;
 IN: math.factorials
 
 [ 1 ] [ -1 factorial ] unit-test ! not necessarily correct
@@ -6,8 +6,12 @@ IN: math.factorials
 [ 1 ] [ 1 factorial ] unit-test
 [ 3628800 ] [ 10 factorial ] unit-test
 
-{ { 1 1 2 3 8 15 48 105 384 945 } }
-[ 10 iota [ double-factorial ] map ] unit-test
+{
+    {
+        1/0. 1/105 1/0. -1/15 1/0. 1/3 1/0. -1 1/0.
+        1 1 1 2 3 8 15 48 105 384 945 3840
+    }
+} [ -10 10 [a,b] [ double-factorial ] map ] unit-test
 
 { 1 } [ 10 10 factorial/ ] unit-test
 { 720 } [ 10 7 factorial/ ] unit-test
index 8760ce537fca7dcc9e97143369a0d65cea31bd3e..699edd66650c74740f3de28abf9f6e3a08f1e1dd 100644 (file)
@@ -12,9 +12,13 @@ MEMO: factorial ( n -- n! )
 ALIAS: n! factorial
 
 MEMO: double-factorial ( n -- n!! )
-    dup 1 > [
-        dup even? 2 1 ? swap 2 <range> product
-    ] [ drop 1 ] if ;
+    dup [ even? ] [ 0 < ] bi [
+        [ drop 1/0. ] [
+            2 + -1 swap -2 <range> product recip
+        ] if
+    ] [
+        2 3 ? swap 2 <range> product
+    ] if ;
 
 ALIAS: n!! double-factorial