tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit c2747457ab8d62e36ae74cd178b75e6fba1c3e45
parent 67a6ce0eee0870842777aa99cd5bca082830a2ed
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sun, 24 Nov 2019 23:56:51 -0800

Preserve number type with sgn

Diffstat:
test.c | 18+++++++++---------
tibs/lib.tsp | 2+-
2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/test.c b/test.c @@ -457,15 +457,15 @@ char *tests[][2] = { { "(abs -3/5)", "3/5" }, { "(abs 0.0)", "0.0" }, - { "sgn", NULL }, - { "(sgn 239)", "1" }, - { "(sgn -3)", "-1" }, - { "(sgn 5/4)", "1" }, - { "(sgn -1/7)", "-1" }, - { "(sgn 3.17)", "1" }, - { "(sgn -.457)", "-1" }, - { "(sgn 0)", "0" }, - { "(sgn 0.0)", "0" }, + { "sgn", NULL }, + { "(sgn 239)", "1" }, + { "(sgn -3)", "-1" }, + { "(sgn 5/4)", "1" }, + { "(sgn -1/7)", "-1" }, + { "(sgn 3.17)", "1.0" }, + { "(sgn -.457)", "-1.0" }, + { "(sgn 0)", "0" }, + { "(sgn 0.0)", "0" }, { "max/min", NULL }, { "(max 9346 13297)", "13297" }, diff --git a/tibs/lib.tsp b/tibs/lib.tsp @@ -221,7 +221,7 @@ (define (arccot x) (/ (arctan x))) (define (abs x) (if (>= x 0) x (- x))) -(define (sgn x) (cond ((= x 0) 0) ((> x 0) 1) (else -1))) +(define (sgn x) (if (= x 0) 0 (/ (abs x) x))) (define (max a b) (if (> a b) a b)) (define (min a b) (if (< a b) a b))