commit bf035d835d0bc3bc6e442b9c1ee483f7df5d94d9
parent 55daf5a35db9741f71d04dde095f2f37a357ec37
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sun, 15 Jan 2023 13:58:51 -0500
lib/math: add "max0"
Diffstat:
3 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/fs/doc/lib/math.txt b/fs/doc/lib/math.txt
@@ -0,0 +1,7 @@
+# Math routines
+
+log2 ( n -- n )
+ Return the logarithm of n to the base 2. Example: $100 log2 == 8
+
+max0 ( n -- n )
+ Return n if n is positive, otherwise 0.
diff --git a/fs/lib/math.fs b/fs/lib/math.fs
@@ -4,3 +4,5 @@
?dup not if abort" log2 can't compute 0" then
32 >r begin ( n )
>> ?dup not if r@ leave then next 32 -^ ( n ) ;
+
+: max0 ( n -- n ) dup 0< if drop 0 then ;
diff --git a/fs/tests/lib/math.fs b/fs/tests/lib/math.fs
@@ -7,5 +7,8 @@ testbegin
2 log2 1 #eq
1 log2 0 #eq
-1 log2 31 #eq
+
+42 max0 42 #eq
+-42 max0 0 #eq
testend