Adding math-compare vocabulary.

db4
John Benediktsson 2008-09-23 14:36:21 -07:00
parent 10d2c8ff7c
commit 78b5e36cac
5 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1,37 @@
! Copyright (C) 2008 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: help.markup help.syntax ;
IN: math.compare
HELP: absmin
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the smaller absolute number with the original sign."
} ;
HELP: absmax
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the larger absolute number with the original sign."
} ;
HELP: posmax
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the most-positive value, or zero if both are negative."
} ;
HELP: negmin
{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the most-negative value, or zero if both are positive."
} ;
HELP: clamp
{ $values { "a" "a number" } { "value" "a number" } { "b" "a number" } { "x" "a number" } }
{ $description
"Returns the value when between 'a' and 'b', 'a' if <= 'a', or 'b' if >= 'b'."
} ;

View File

@ -0,0 +1,25 @@
USING: kernel math math.functions math.compare tools.test ;
IN: math.compare.tests
[ -1 ] [ -1 5 absmin ] unit-test
[ -1 ] [ -1 -5 absmin ] unit-test
[ -5 ] [ 1 -5 absmax ] unit-test
[ 5 ] [ 1 5 absmax ] unit-test
[ 0 ] [ -1 -3 posmax ] unit-test
[ 1 ] [ 1 -3 posmax ] unit-test
[ 3 ] [ -1 3 posmax ] unit-test
[ 0 ] [ 1 3 negmin ] unit-test
[ -3 ] [ 1 -3 negmin ] unit-test
[ -1 ] [ -1 3 negmin ] unit-test
[ 0 ] [ 0 -1 2 clamp ] unit-test
[ 1 ] [ 0 1 2 clamp ] unit-test
[ 2 ] [ 0 3 2 clamp ] unit-test

View File

@ -0,0 +1,19 @@
USING: math math.order kernel ;
IN: math.compare
: absmin ( a b -- x )
[ [ abs ] dip abs < ] 2keep ? ;
: absmax ( a b -- x )
[ [ abs ] dip abs > ] 2keep ? ;
: posmax ( a b -- x )
0 max max ;
: negmin ( a b -- x )
0 min min ;
: clamp ( a value b -- x )
min max ;

View File

@ -0,0 +1 @@
Comparison functions.