Adding math-compare vocabulary.
							parent
							
								
									10d2c8ff7c
								
							
						
					
					
						commit
						78b5e36cac
					
				| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					John Benediktsson
 | 
				
			||||||
| 
						 | 
					@ -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'."
 | 
				
			||||||
 | 
					} ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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 ; 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					Comparison functions.
 | 
				
			||||||
		Loading…
	
		Reference in New Issue