aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Number
diff options
context:
space:
mode:
authorMarvin Borner2023-06-02 23:48:47 +0200
committerMarvin Borner2023-06-02 23:48:47 +0200
commit9186091f3d5ef244118d13921d21747f33c94bb3 (patch)
tree707dba519815304b2049ec346866cb3959a0fa6c /std/Number
parent5a7e7199354a5b6711eeafd384f643ec4e7dc842 (diff)
Added some missing functions
Diffstat (limited to 'std/Number')
-rw-r--r--std/Number/Binary.bruijn43
-rw-r--r--std/Number/Ternary.bruijn20
2 files changed, 50 insertions, 13 deletions
diff --git a/std/Number/Binary.bruijn b/std/Number/Binary.bruijn
index 2900414..ea837e3 100644
--- a/std/Number/Binary.bruijn
+++ b/std/Number/Binary.bruijn
@@ -194,15 +194,15 @@ ternary! z [[[rec]]] (+0t) ⧗ Binary → Ternary
# flips the bits of a binary number (1's complement)
complement [[[[3 2 0 1]]]] ⧗ Binary → Binary
-~‣ complement
+-*‣ complement
-:test (~(+0b) =? (+0b)) (true)
-:test (~(+1b) =? (+0b)) (true)
-:test (~(+42b)) ([[[1 (0 (1 (0 (1 (0 2)))))]]])
+:test (-*(+0b) =? (+0b)) (true)
+:test (-*(+1b) =? (+0b)) (true)
+:test (-*(+42b)) ([[[1 (0 (1 (0 (1 (0 2)))))]]])
# inverts a binary number by complementing and incrementing (2's complement)
# don't forget to pad the number with zeroes if needed
-invert ++‣ ∘ ~‣ ⧗ Binary → Binary
+invert ++‣ ∘ -*‣ ⧗ Binary → Binary
-‣ invert
@@ -261,3 +261,36 @@ sub [[(0 =? 1) (+0b) -((pad 1 0) + -(pad 0 1))]] ⧗ Binary → Binary → Binar
:test ((+42b) - (+12b) =? (+30b)) (true)
:test ((+3b) - (+0b) =? (+3b)) (true)
:test ((+3b) - (+2b) =? (+1b)) (true)
+
+# TODO: mul/div
+
+# rshifts least significant bit of a binary number
+div² [~(0 z a¹ a⁰)] ⧗ Binary → Binary
+ z (+0b) : (+0b)
+ a¹ [0 [[↑¹1 : 1]]]
+ a⁰ [0 [[↑⁰1 : 1]]]
+
+/²‣ div²
+
+:test (/²(+6b) =? (+3b)) (true)
+:test (/²(+5b) =? (+2b)) (true)
+
+# returns true if the number is even (remainder mod 2 == 0)
+even? ¬‣ ∘ lst ⧗ Binary → Boolean
+
+=²?‣ even?
+
+:test (even? (+0b)) (true)
+:test (even? (+1b)) (false)
+:test (even? (+41b)) (false)
+:test (even? (+42b)) (true)
+
+# returns true if the number is odd (remainder mod 2 == 1)
+odd? lst ⧗ Binary → Boolean
+
+≠²?‣ odd?
+
+:test (odd? (+0b)) (false)
+:test (odd? (+1b)) (true)
+:test (odd? (+41b)) (true)
+:test (odd? (+42b)) (false)
diff --git a/std/Number/Ternary.bruijn b/std/Number/Ternary.bruijn
index f115349..86128ca 100644
--- a/std/Number/Ternary.bruijn
+++ b/std/Number/Ternary.bruijn
@@ -451,18 +451,22 @@ even? z [[rec]] ⧗ Number → Boolean
case-rec t⁰? (lst 0) (1 /³0) ¬(1 /³0)
case-end true
-:test (even? (+0)) (true)
-:test (even? (+1)) (false)
-:test (even? (+41)) (false)
-:test (even? (+42)) (true)
+=²?‣ even?
+
+:test (=²?(+0)) (true)
+:test (=²?(+1)) (false)
+:test (=²?(+41)) (false)
+:test (=²?(+42)) (true)
# returns true if the number is odd (remainder mod 2 == 1)
odd? ¬‣ ∘ even? ⧗ Number → Boolean
-:test (odd? (+0)) (false)
-:test (odd? (+1)) (true)
-:test (odd? (+41)) (true)
-:test (odd? (+42)) (false)
+≠²?‣ odd?
+
+:test (≠²?(+0)) (false)
+:test (≠²?(+1)) (true)
+:test (≠²?(+41)) (true)
+:test (≠²?(+42)) (false)
# returns max number of two
max [[(1 ≤? 0) 0 1]] ⧗ Number → Number → Number