diff options
-rw-r--r-- | mew.scm | 6 | ||||
-rw-r--r-- | mew.svnwiki | 4 | ||||
-rw-r--r-- | tests/test.mew | 7 |
3 files changed, 16 insertions, 1 deletions
diff --git a/mew.scm b/mew.scm index a6ac371..f95c0b1 100644 --- a/mew.scm +++ b/mew.scm @@ -18,7 +18,7 @@ odometer one-of op op* per prn proj puts rand range rep - sample scan scan-right sing? search seq set set-at + sample scan scan-right sing? search seq set set-at sgn shuffle shuffle! str slurp tally-accumulator tbl time while @@ -105,6 +105,10 @@ (reexport err) + (define (sgn n) + (cond ((< n 0) -1) + ((> n 0) 1) + (else 0))) ; always return an integer 0, not n (define (inc i) (+ i 1)) diff --git a/mew.svnwiki b/mew.svnwiki index 29eb4e9..11a2669 100644 --- a/mew.svnwiki +++ b/mew.svnwiki @@ -152,6 +152,10 @@ Alias for {{floor-quotient}}. Alias for {{floor-remainder}}. +<procedure>(sgn <num>)</procedure> + +Returns -1, 0, 1 depending on whether {{<num>}} is negative, zero, or positve. + == General helpers diff --git a/tests/test.mew b/tests/test.mew index 1fe7ec1..72d1e27 100644 --- a/tests/test.mew +++ b/tests/test.mew @@ -83,6 +83,13 @@ (test 5 (mod -16 7)) (test -2 (mod -16 -7))) +(test-group "sgn" + (test -1 (sgn -42)) + (test 0 (sgn 0)) + (test #t (eq? 0 (sgn 0.0))) + (test #t (eq? 0 (sgn -0.0))) + (test 1 (sgn 42))) + (test-group "inc" (test 6 (inc 5)) (test 0 (inc -1)) |