From 893c620df9481c9b36017687102640d64554d776 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Thu, 13 Jun 2024 09:06:32 -0300 Subject: [PATCH 1/5] Add log and atan2 as builtin functions --- src/fun/builtins.bend | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index 4b1de122..b5b4e39d 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -108,4 +108,9 @@ sleep hi_lo = (IO/Call IO/MAGIC "PUT_TIME" hi_lo @x (IO/Done IO/MAGIC x)) # (defer_arg (defer_arg (defer_arg (defer @arg1 @arg2 @arg3 (f arg1 arg2 arg3)) arg1) arg2) arg3) defer val = @x (x val) defer_arg defered arg = @x (defered x arg) -undefer defered = (defered @x x) \ No newline at end of file +undefer defered = (defered @x x) + +# log :: f24 -> f24 -> f24 +log x base = (| base x) +# atan2 :: f24 -> f24 -> f24 +atan2 x y = (& x y) From 88a3a310d65b85bbbed47de8c37566ea35cb8cf3 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Thu, 13 Jun 2024 09:11:14 -0300 Subject: [PATCH 2/5] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 249d27f7..d6716bd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ and this project does not currently adhere to a particular versioning scheme. - Fixed readback of numeric operations. ([#467][gh-467]) +### Added + +- `log` and `atan2` builtin functions. ([#583][gh-583]) + ## [0.2.35] - 2024-06-06 ### Changed @@ -335,4 +339,5 @@ and this project does not currently adhere to a particular versioning scheme. [gh-516]: https://github.com/HigherOrderCO/Bend/issues/516 [gh-526]: https://github.com/HigherOrderCO/Bend/issues/526 [gh-528]: https://github.com/HigherOrderCO/Bend/issues/528 +[gh-583]: https://github.com/HigherOrderCO/Bend/issues/583 [Unreleased]: https://github.com/HigherOrderCO/Bend/compare/0.2.35...HEAD From bc431426492a936262ec7600ba2c730da91edab4 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Thu, 13 Jun 2024 09:59:33 -0300 Subject: [PATCH 3/5] Update docs --- docs/native-numbers.md | 50 +++++++++++++++++++++--------------------- src/fun/builtins.bend | 1 + 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/docs/native-numbers.md b/docs/native-numbers.md index db6233b6..cd88e3a2 100644 --- a/docs/native-numbers.md +++ b/docs/native-numbers.md @@ -6,7 +6,6 @@ Currently Bend supports 3 types of native numbers for fast numeric operations (c - I24: Signed integers (24 bits, two's complement) - F24: Floating point numbers (single precision IEEE-754 floating point with the last bits of the mantissa implicitly set to zero) - ### U24 Unsigned numbers are written as just the number and are represented as a 24 bit unsigned integer. @@ -15,7 +14,6 @@ Unsigned numbers are written as just the number and are represented as a 24 bit two = 2 ``` - ### I24 Signed numbers are written with a `+` or `-` sign and are represented as a 24 bit two's complement integer. @@ -29,7 +27,7 @@ Positive numbers _must_ be written with a `+` sign, otherwise they'll be interpr Numbers can also be written in binary or hexadecimal form. Underscores can be optionally used as digit separators to make large numbers more readable. -```rs +````rs decimal = 1194684 binary = 0b100_100_011_101_010_111_100 hexadecimal = 0x123_abc @@ -47,8 +45,7 @@ pi = +3.1415926535897932384626433 # Will get rounded to 24bit float a_millionth = 0.000001 zero = 0.0 minus_zero = -0.0 -``` - +```` ### Mixing number types @@ -61,7 +58,6 @@ During runtime, the executed numeric function depends on both the type tag and t At the moment Bend doesn't have a way to convert between the different number types, but it will be added in the future. - ### Operations There is also support for native operations. @@ -75,24 +71,30 @@ some_val = (+ (+ 7 4) (* 2 3)) These are the currently available operations: -Operation | Description | Accepted types | Return type -----------|-------------|----------------|------------ -\+ | Addition | U24, I24, F24 | Same as arguments -\- | Subtraction | U24, I24, F24 | Same as arguments -\* | Multiplication | U24, I24, F24 | Same as arguments -\/ | Division | U24, I24, F24 | Same as arguments -\% | Modulo | U24, I24, F24 | Same as arguments -\== | Equality | U24, I24, F24 | U24 -\!= | Inequality | U24, I24, F24 | U24 -\< | Less than | U24, I24, F24 | U24 -\<= | Less than or equal to | U24, I24, F24 | U24 -\> | Greater than | U24, I24, F24 | U24 -\>= | Greater than or equal to | U24, I24, F24 | U24 -\& | Bitwise and | U24, I24 | Same as arguments -\| | Bitwise or | U24, I24 | Same as arguments -\^ | Bitwise xor | U24, I24 | Same as arguments -\** | Exponentiation | F24 | F24 +| Operation | Description | Accepted types | Return type | +| --------- | ------------------------ | -------------- | ----------------- | +| \+ | Addition | U24, I24, F24 | Same as arguments | +| \- | Subtraction | U24, I24, F24 | Same as arguments | +| \* | Multiplication | U24, I24, F24 | Same as arguments | +| \/ | Division | U24, I24, F24 | Same as arguments | +| \% | Modulo | U24, I24, F24 | Same as arguments | +| \== | Equality | U24, I24, F24 | U24 | +| \!= | Inequality | U24, I24, F24 | U24 | +| \< | Less than | U24, I24, F24 | U24 | +| \<= | Less than or equal to | U24, I24, F24 | U24 | +| \> | Greater than | U24, I24, F24 | U24 | +| \>= | Greater than or equal to | U24, I24, F24 | U24 | +| \& | Bitwise and | U24, I24 | Same as arguments | +| \| | Bitwise or | U24, I24 | Same as arguments | +| \^ | Bitwise xor | U24, I24 | Same as arguments | +| \*\* | Exponentiation | F24 | F24 | +### Functions + +| Name | Description | Accepted types | Return type | +| -------------- | ------------------------------- | -------------- | ----------- | +| `log(x, base)` | Logarithm | F24 | F24 | +| `atan2(x, y)` | 2 arguments arctangent (atan2f) | F24 | F24 | ### Pattern matching @@ -122,7 +124,6 @@ Number.minus_three = λn λf λx } ``` - Using everything we learned, we can write a program that calculates the n-th Fibonacci number using native numbers: ```py @@ -140,7 +141,6 @@ fibonacci = λn # n is the argument main = (fibonacci 15) ``` - ### Pattern matching numbers in Fun syntax equations In Fun syntax, we can also use pattern matching equations to match on native unsigned numbers. diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index b5b4e39d..4627e896 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -113,4 +113,5 @@ undefer defered = (defered @x x) # log :: f24 -> f24 -> f24 log x base = (| base x) # atan2 :: f24 -> f24 -> f24 +# Has the same behaviour as `atan2f` in the C math lib. atan2 x y = (& x y) From f8811850a76e4fcadf6e6d79b8c2400ddc52b007 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Thu, 13 Jun 2024 10:39:07 -0300 Subject: [PATCH 4/5] Update num ops test --- tests/golden_tests/run_file/basic_num_ops.bend | 13 +++++++++---- tests/snapshots/run_file__basic_num_ops.bend.snap | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/golden_tests/run_file/basic_num_ops.bend b/tests/golden_tests/run_file/basic_num_ops.bend index 961ce351..3264972e 100644 --- a/tests/golden_tests/run_file/basic_num_ops.bend +++ b/tests/golden_tests/run_file/basic_num_ops.bend @@ -15,8 +15,8 @@ main = (List/expand (!= 20 10) (< 20 10) (> 20 10) - #(<< 10 2) - #(>> 10 2) + (<< 10 2) + (>> 10 2) 0xFFFF @@ -77,7 +77,7 @@ main = (List/expand (!= -20 +10) (< -20 +10) (> -20 +10) - + 0xFFFF (+ +20.0 +10.0) @@ -137,5 +137,10 @@ main = (List/expand (!= -20.0 +10.0) (< -20.0 +10.0) (> -20.0 +10.0) + + 0xFFFF + + (log 2.0 3.0) + (atan2 3.0 4.0) ] -) \ No newline at end of file +) diff --git a/tests/snapshots/run_file__basic_num_ops.bend.snap b/tests/snapshots/run_file__basic_num_ops.bend.snap index fd68f633..d3e09f45 100644 --- a/tests/snapshots/run_file__basic_num_ops.bend.snap +++ b/tests/snapshots/run_file__basic_num_ops.bend.snap @@ -3,7 +3,7 @@ source: tests/golden_tests.rs input_file: tests/golden_tests/run_file/basic_num_ops.bend --- NumScott: -[30, 10, 200, 2, 0, 30, 0, 30, 0, 1, 0, 1, 65535, +30, +10, +200, +2, +0, +30, +0, +30, 0, 1, 0, 1, 65535, -30, -10, +200, +2, +0, +26, -28, -2, 0, 1, 1, 0, 65535, +10, +30, -200, -2, +0, -30, +20, -10, 0, 1, 0, 1, 65535, -10, -30, -200, -2, +0, -26, +8, -18, 0, 1, 1, 0, 65535, 30.000, 10.000, 200.000, 2.000, 0.000, 10240007340032.000, 1.107, 0.769, 0, 1, 0, 1, 65535, -30.000, -10.000, 200.000, 2.000, -0.000, 0.000, -2.034, NaN, 0, 1, 1, 0, 65535, 10.000, 30.000, -200.000, -2.000, 0.000, 0.000, 2.034, NaN, 0, 1, 0, 1, 65535, -10.000, -30.000, -200.000, -2.000, -0.000, 10240007340032.000, -1.107, NaN, 0, 1, 1, 0] +[30, 10, 200, 2, 0, 30, 0, 30, 0, 1, 0, 1, 40, 2, 65535, +30, +10, +200, +2, +0, +30, +0, +30, 0, 1, 0, 1, 65535, -30, -10, +200, +2, +0, +26, -28, -2, 0, 1, 1, 0, 65535, +10, +30, -200, -2, +0, -30, +20, -10, 0, 1, 0, 1, 65535, -10, -30, -200, -2, +0, -26, +8, -18, 0, 1, 1, 0, 65535, 30.000, 10.000, 200.000, 2.000, 0.000, 10240007340032.000, 1.107, 0.769, 0, 1, 0, 1, 65535, -30.000, -10.000, 200.000, 2.000, -0.000, 0.000, -2.034, NaN, 0, 1, 1, 0, 65535, 10.000, 30.000, -200.000, -2.000, 0.000, 0.000, 2.034, NaN, 0, 1, 0, 1, 65535, -10.000, -30.000, -200.000, -2.000, -0.000, 10240007340032.000, -1.107, NaN, 0, 1, 1, 0, 65535, 0.631, 0.643] Scott: -[30, 10, 200, 2, 0, 30, 0, 30, 0, 1, 0, 1, 65535, +30, +10, +200, +2, +0, +30, +0, +30, 0, 1, 0, 1, 65535, -30, -10, +200, +2, +0, +26, -28, -2, 0, 1, 1, 0, 65535, +10, +30, -200, -2, +0, -30, +20, -10, 0, 1, 0, 1, 65535, -10, -30, -200, -2, +0, -26, +8, -18, 0, 1, 1, 0, 65535, 30.000, 10.000, 200.000, 2.000, 0.000, 10240007340032.000, 1.107, 0.769, 0, 1, 0, 1, 65535, -30.000, -10.000, 200.000, 2.000, -0.000, 0.000, -2.034, NaN, 0, 1, 1, 0, 65535, 10.000, 30.000, -200.000, -2.000, 0.000, 0.000, 2.034, NaN, 0, 1, 0, 1, 65535, -10.000, -30.000, -200.000, -2.000, -0.000, 10240007340032.000, -1.107, NaN, 0, 1, 1, 0] +[30, 10, 200, 2, 0, 30, 0, 30, 0, 1, 0, 1, 40, 2, 65535, +30, +10, +200, +2, +0, +30, +0, +30, 0, 1, 0, 1, 65535, -30, -10, +200, +2, +0, +26, -28, -2, 0, 1, 1, 0, 65535, +10, +30, -200, -2, +0, -30, +20, -10, 0, 1, 0, 1, 65535, -10, -30, -200, -2, +0, -26, +8, -18, 0, 1, 1, 0, 65535, 30.000, 10.000, 200.000, 2.000, 0.000, 10240007340032.000, 1.107, 0.769, 0, 1, 0, 1, 65535, -30.000, -10.000, 200.000, 2.000, -0.000, 0.000, -2.034, NaN, 0, 1, 1, 0, 65535, 10.000, 30.000, -200.000, -2.000, 0.000, 0.000, 2.034, NaN, 0, 1, 0, 1, 65535, -10.000, -30.000, -200.000, -2.000, -0.000, 10240007340032.000, -1.107, NaN, 0, 1, 1, 0, 65535, 0.631, 0.643] From f11af644d11c8c3104a13b5a0dce2683320234f4 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Thu, 13 Jun 2024 13:48:34 -0300 Subject: [PATCH 5/5] Improve log and atan2 code comments --- src/fun/builtins.bend | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index 4627e896..822eef05 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -111,7 +111,9 @@ defer_arg defered arg = @x (defered x arg) undefer defered = (defered @x x) # log :: f24 -> f24 -> f24 +# Computes the logarithm of `x` with the specified `base`. log x base = (| base x) # atan2 :: f24 -> f24 -> f24 # Has the same behaviour as `atan2f` in the C math lib. +# Computes the arctangent of the quotient of its two arguments. atan2 x y = (& x y)