diff options
author | Marvin Borner | 2024-04-24 01:13:49 +0200 |
---|---|---|
committer | Marvin Borner | 2024-04-24 01:13:49 +0200 |
commit | c8245b83c88dfb551556d2029d435bf5eb00d71b (patch) | |
tree | 1d3482664e893a6a18af30f4eedcf02a117246f9 | |
parent | bfdd0ef6897e42e03cefa4d43c8757ef09d96de2 (diff) |
More tests
-rw-r--r-- | std/Math/Complex.bruijn | 6 | ||||
-rw-r--r-- | std/Math/Real.bruijn | 46 |
2 files changed, 41 insertions, 11 deletions
diff --git a/std/Math/Complex.bruijn b/std/Math/Complex.bruijn index ec83462..a20717f 100644 --- a/std/Math/Complex.bruijn +++ b/std/Math/Complex.bruijn @@ -7,6 +7,12 @@ ι (+0.0r) : (+1.0r) +# returns true of two complex numbers are equal approximately +approx-eq? [[[R.approx-eq? 2 (1 2) (0 2)]]] ⧗ Number → Complex → Complex → Boolean + +# TODO: bigger value (higher performance first!) +…≈?… approx-eq? (+1000) + # adds two complex numbers add &[[&[[(R.add 3 1) : (R.add 2 0)]]]] ⧗ Complex → Complex → Complex diff --git a/std/Math/Real.bruijn b/std/Math/Real.bruijn index 2bd004a..b375f13 100644 --- a/std/Math/Real.bruijn +++ b/std/Math/Real.bruijn @@ -3,15 +3,17 @@ :import std/Logic . :import std/Combinator . -:import std/Math/Rational . +:import std/Pair . +:import std/Math/Rational Q :import std/Number N # converts a balanced ternary number to a real number -number→real [[number→rational 1]] ⧗ Number → Real +number→real [[Q.number→rational 1]] ⧗ Number → Real :test (number→real (+5)) ((+5.0r)) -approx-eq? [[[eq? (1 2) (0 2)]]] ⧗ Number → Real → Real → Boolean +# returns true if two real numbers are equal approximately +approx-eq? [[[Q.eq? (1 2) (0 2)]]] ⧗ Number → Real → Real → Boolean # TODO: bigger value (higher performance first!) …≈?… approx-eq? (+1000) @@ -23,43 +25,65 @@ approx-eq? [[[eq? (1 2) (0 2)]]] ⧗ Number → Real → Real → Boolean φ-lim [[[[[N.=?0 ε (4 (3 0) (2 0))] N.--0]]]] # extends b combinator by canceling further reductions when approximator hits 0 -b-lim [[[N.=?0 ε (2 (1 N.--0))]]] +b-lim [[[[N.=?0 ε (3 (2 0))] N.--0]]] # adds two real numbers -add φ-lim add ⧗ Real → Real → Real +add φ-lim Q.add ⧗ Real → Real → Real …+… add +:test ((+1.0r) + (+0.0r) ≈? (+1.0r)) (true) +:test ((+0.0r) + (-1.0r) ≈? (-1.0r)) (true) + # subtracts two real numbers -sub φ-lim sub ⧗ Real → Real → Real +sub φ-lim Q.sub ⧗ Real → Real → Real …-… sub +:test ((+1.0r) - (+0.5r) ≈? (+0.5r)) (true) +:test ((+0.0r) - (-1.0r) ≈? (+1.0r)) (true) + # multiplies two real numbers -mul φ-lim mul ⧗ Real → Real → Real +mul φ-lim Q.mul ⧗ Real → Real → Real …⋅… mul +:test ((+5.0r) ⋅ (+5.0r) ≈? (+25.0r)) (true) +:test ((+1.8r) ⋅ (+1.2r) ≈? (+2.16r)) (true) + # divides two real numbers -div φ-lim div ⧗ Real → Real → Real +div φ-lim Q.div ⧗ Real → Real → Real …/… div +:test ((+8.0r) / (+4.0r) ≈? (+2.0r)) (true) +:test ((+18.0r) / (+12.0r) ≈? (+1.5r)) (true) + # negates a real number -negate b-lim negate ⧗ Real → Real +negate b-lim Q.negate ⧗ Real → Real -‣ negate +:test (-(+0.0r) ≈? (+0.0r)) (true) +:test (-(+4.2r) ≈? (-4.2r)) (true) +:test (-(-4.2r) ≈? (+4.2r)) (true) + # inverts a real number -invert b-lim invert ⧗ Real → Real +invert b-lim Q.invert ⧗ Real → Real ~‣ invert +:test (~(+0.5r) ≈? (+2.0r)) (true) +:test (~(-0.5r) ≈? (-2.0r)) (true) + # finds smallest equivalent representation of a real number -compress b-lim compress ⧗ Real → Real +compress b-lim Q.compress ⧗ Real → Real %‣ compress +:test (%[(+4) : (+1)] ≈? (+2.0r)) (true) +:test (%[(+4) : (+7)] ≈? (+0.5r)) (true) + # --- :import std/List . |