From 1fb92f42ab77da5311e547ddb56de3e5d3cbf988 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 13 Apr 2024 14:17:48 +0200 Subject: Added initial implementation for floats/rationals --- std/Math/Rational.bruijn | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 std/Math/Rational.bruijn (limited to 'std/Math/Rational.bruijn') diff --git a/std/Math/Rational.bruijn b/std/Math/Rational.bruijn new file mode 100644 index 0000000..4c2f046 --- /dev/null +++ b/std/Math/Rational.bruijn @@ -0,0 +1,87 @@ +# ideas by u/DaVinci103 +# MIT License, Copyright (c) 2024 Marvin Borner + +# (p : q) ⇔ (1 / (q + 1)) + +:import std/Combinator . +:import std/Logic . +:import std/Pair . +:import std/Math N + +# returns true if two rational numbers are equal +eq? &[[&[[N.eq? (N.mul 3 N.++0) (N.mul N.++2 1)]]]] ⧗ Rational → Rational → Boolean + +…=?… eq? + +:test (((+1) : (+3)) =? ((+2) : (+7))) (true) +:test ((+0.5) =? (+0.5)) (true) +:test ((+42.0) =? (+42.0)) (true) +:test ((+0.4) =? (+0.5)) (false) + +# finds smallest equivalent representation of a rational number +compress &[[[(N.div 2 0) : N.--(N.div N.++1 0)] (N.gcd 1 N.++0)]] ⧗ Rational → Rational + +%‣ compress + +:test (%((+4) : (+1)) =? (+2.0)) (true) +:test (%((+4) : (+7)) =? (+0.5)) (true) + +# adds two rational numbers +add &[[&[[p : q]]]] ⧗ Rational → Rational → Rational + p N.add (N.mul 3 N.++0) (N.mul 1 N.++2) + q N.add (N.mul 2 0) (N.add 2 0) + +…+… add + +:test ((+0.5) + (+0.5) =? (+1.0)) (true) +:test ((+1.8) + (+1.2) =? (+3.0)) (true) +:test ((-1.8) + (+1.2) =? (-0.6)) (true) + +# subtracts two rational numbers +sub &[[&[[p : q]]]] ⧗ Rational → Rational → Rational + p N.sub (N.mul 3 N.++0) (N.mul 1 N.++2) + q N.add (N.mul 2 0) (N.add 2 0) + +…-… sub + +:test ((+0.5) - (+0.5) =? (+0.0)) (true) +:test ((+3.0) - (+1.8) =? (+1.2)) (true) +:test ((+1.8) - (-1.2) =? (+3.0)) (true) + +# negates a rational number +negate &[[N.-1 : 0]] ⧗ Rational → Rational + +-‣ negate + +:test (-(+0.0) =? (+0.0)) (true) +:test (-(+4.2) =? (-4.2)) (true) +:test (-(-4.2) =? (+4.2)) (true) + +# multiplies two rational numbers +mul &[[&[[p : q]]]] ⧗ Rational → Rational → Rational + p N.mul 3 1 + q N.add (N.mul 2 0) (N.add 2 0) + +…⋅… mul + +:test ((+5.0) ⋅ (+5.0) =? (+25.0)) (true) +:test ((+1.8) ⋅ (+1.2) =? (+2.16)) (true) + +# finds the multiplicative inverse of a rational number +invert &[[N.compare-case eq gt lt 1 (+0)]] ⧗ Rational → Rational + eq Ω + gt N.++0 : N.--1 + lt N.-(N.++0) : N.--(N.-1) + +~‣ invert + +:test (~(+0.5) =? (+2.0)) (true) +:test (~(-0.5) =? (-2.0)) (true) + +# divides two rational numbers +div [[1 ⋅ ~0]] ⧗ Rational → Rational → Rational + +…/… div + +:test ((+8.0) / (+4.0) =? (+2.0)) (true) +:test ((+18.0) / (+12.0) =? (+1.5)) (true) -- cgit v1.2.3