diff options
author | Marvin Borner | 2023-02-26 15:24:50 +0100 |
---|---|---|
committer | Marvin Borner | 2023-02-26 15:24:50 +0100 |
commit | 2d4c48368bd6d2ccca7302e5eb0b0ba3f3cd6581 (patch) | |
tree | 99a8447955fc1aff9e9d496755802708ea24da2d | |
parent | ddadb33e5cdd8be886a8130222c46dabc084e83c (diff) |
More std functions
-rw-r--r-- | std/Float.bruijn | 16 | ||||
-rw-r--r-- | std/Math.bruijn | 6 | ||||
-rw-r--r-- | std/Number/Unary.bruijn | 32 |
3 files changed, 43 insertions, 11 deletions
diff --git a/std/Float.bruijn b/std/Float.bruijn new file mode 100644 index 0000000..d3fce4e --- /dev/null +++ b/std/Float.bruijn @@ -0,0 +1,16 @@ +# MIT License, Copyright (c) 2022 Marvin Borner +# Arbitrary-precision floating-point arithmetic implementation using +# (+3.14) = pair (+3) (+14) + +:import std/Combinator . +:import std/Number . +:import std/List . +:import std/Pair P + +# generates a float from a normal balanced ternary number +float! \(P.:) (+0) + +# adds two floating numbers +# TODO: Carry support +# - needed: mod, div (?) -> ternary carry != decimal carry +add P.zip-with (+) diff --git a/std/Math.bruijn b/std/Math.bruijn index 31dd572..d63fb2f 100644 --- a/std/Math.bruijn +++ b/std/Math.bruijn @@ -58,9 +58,9 @@ gcd z [[[(1 =? 0) case-eq ((1 >? 0) case-gre case-les)]]] ⧗ Number → Number case-gre 2 (1 - 0) 0 case-les 2 1 (0 - 1) -:test ((gcd (+2) (+4)) =? ((+2))) (true) -:test ((gcd (+10) (+5)) =? ((+5))) (true) -:test ((gcd (+3) (+8)) =? ((+1))) (true) +:test ((gcd (+2) (+4)) =? (+2)) (true) +:test ((gcd (+10) (+5)) =? (+5)) (true) +:test ((gcd (+3) (+8)) =? (+1)) (true) # power function pow […!!… (iterate (…⋅… 0) (+1))] ⧗ Number → Number → Number diff --git a/std/Number/Unary.bruijn b/std/Number/Unary.bruijn index 244eefb..edc9dcd 100644 --- a/std/Number/Unary.bruijn +++ b/std/Number/Unary.bruijn @@ -1,14 +1,13 @@ # MIT License, Copyright (c) 2022 Marvin Borner +# with help from Justine Tunney # classic Church style numerals :import std/Logic . :import std/Combinator . :import std/Pair . -zero [[0]] - # returns true if a unary number is zero -zero? [0 [zero] true] ⧗ Unary → Boolean +zero? [0 [(+0u)] true] ⧗ Unary → Boolean =?‣ zero? @@ -114,8 +113,7 @@ mul …∘… ⧗ Unary → Unary → Unary :test ((+2u) ⋅ (+3u)) ((+6u)) # divs two unary numbers -div [z div* ++0] ⧗ Unary → Unary → Unary - div* [[[[[[=?0 ((+0u) 2 1) (2 (5 0 3 2 1))] (3 - 2)]]]]] +div [[[[3 [[0 1]] [1] (3 [3 [[0 1]] [3 (0 1)] [0]] 0)]]]] ⧗ Unary → Unary → Unary …/… div @@ -124,16 +122,23 @@ div [z div* ++0] ⧗ Unary → Unary → Unary :test ((+2u) / (+2u)) ((+1u)) :test ((+2u) / (+3u)) ((+0u)) +# slower div +div* [z rec ++0] ⧗ Unary → Unary → Unary + rec [[[[[[=?0 ((+0u) 2 1) (2 (5 0 3 2 1))] (3 - 2)]]]]] + # returns remainder of integer division -mod [[1 [0 [[(0 ⋀? (3 ≤? 1)) case-rec case-end]]] (1 : true) [[1]]]] ⧗ Unary → Unary → Unary - case-rec (1 - 3) : true - case-end 1 : false +mod [[[[3 [0 [[1]]] (3 [3 [[[0 (2 (5 1)) 1]]] [1] 1] [1]) [[0]]]]]] ⧗ Unary → Unary → Unary …%… mod :test ((+10u) % (+3u)) ((+1u)) :test ((+3u) % (+5u)) ((+3u)) +# slower mod +mod* [[1 [0 [[(0 ⋀? (3 ≤? 1)) case-rec case-end]]] (1 : true) [[1]]]] ⧗ Unary → Unary → Unary + case-rec (1 - 3) : true + case-end 1 : false + # exponentiates two unary number # doesn't give correct results for x^0 pow* [[1 0]] ⧗ Unary → Unary → Unary @@ -145,3 +150,14 @@ pow [[0 [[3 (1 0)]] [[1 0]]]] ⧗ Unary → Unary → Unary :test ((+2u) ^ (+3u)) ((+8u)) :test ((+3u) ^ (+2u)) ((+9u)) + +# fibonacci sequence +# index +1 vs std/Math fib +fib [0 [[[2 0 [2 (1 0)]]]] [[1]] [0]] ⧗ Unary → Unary + +:test (fib (+6u)) ((+8u)) + +# factorial function +fac [[1 [[0 (1 [[2 1 (1 0)]])]] [1] [0]]] ⧗ Unary → Unary + +:test (fac (+3u)) ((+6u)) |