Merge pull request #585 from HigherOrderCO/583-add-log-and-atan2-operations-for-floats

#583 Add log and atan2 operations for floats
This commit is contained in:
Nicolas Abril 2024-06-13 18:13:38 +00:00 committed by GitHub
commit 4d43bd9405
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 50 additions and 32 deletions

View File

@ -16,6 +16,10 @@ and this project does not currently adhere to a particular versioning scheme.
- Fixed readback of numeric operations. ([#467][gh-467]) - Fixed readback of numeric operations. ([#467][gh-467])
### Added
- `log` and `atan2` builtin functions. ([#583][gh-583])
## [0.2.35] - 2024-06-06 ## [0.2.35] - 2024-06-06
### Changed ### 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-516]: https://github.com/HigherOrderCO/Bend/issues/516
[gh-526]: https://github.com/HigherOrderCO/Bend/issues/526 [gh-526]: https://github.com/HigherOrderCO/Bend/issues/526
[gh-528]: https://github.com/HigherOrderCO/Bend/issues/528 [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 [Unreleased]: https://github.com/HigherOrderCO/Bend/compare/0.2.35...HEAD

View File

@ -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) - 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) - F24: Floating point numbers (single precision IEEE-754 floating point with the last bits of the mantissa implicitly set to zero)
### U24 ### U24
Unsigned numbers are written as just the number and are represented as a 24 bit unsigned integer. 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 two = 2
``` ```
### I24 ### I24
Signed numbers are written with a `+` or `-` sign and are represented as a 24 bit two's complement integer. 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. 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 decimal = 1194684
binary = 0b100_100_011_101_010_111_100 binary = 0b100_100_011_101_010_111_100
hexadecimal = 0x123_abc hexadecimal = 0x123_abc
@ -47,8 +45,7 @@ pi = +3.1415926535897932384626433 # Will get rounded to 24bit float
a_millionth = 0.000001 a_millionth = 0.000001
zero = 0.0 zero = 0.0
minus_zero = -0.0 minus_zero = -0.0
``` ````
### Mixing number types ### 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. 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 ### Operations
There is also support for native operations. There is also support for native operations.
@ -75,24 +71,30 @@ some_val = (+ (+ 7 4) (* 2 3))
These are the currently available operations: These are the currently available operations:
Operation | Description | Accepted types | Return type | Operation | Description | Accepted types | Return type |
----------|-------------|----------------|------------ | --------- | ------------------------ | -------------- | ----------------- |
\+ | Addition | U24, I24, F24 | Same as arguments | \+ | Addition | U24, I24, F24 | Same as arguments |
\- | Subtraction | U24, I24, F24 | Same as arguments | \- | Subtraction | U24, I24, F24 | Same as arguments |
\* | Multiplication | U24, I24, F24 | Same as arguments | \* | Multiplication | U24, I24, F24 | Same as arguments |
\/ | Division | U24, I24, F24 | Same as arguments | \/ | Division | U24, I24, F24 | Same as arguments |
\% | Modulo | U24, I24, F24 | Same as arguments | \% | Modulo | U24, I24, F24 | Same as arguments |
\== | Equality | U24, I24, F24 | U24 | \== | Equality | U24, I24, F24 | U24 |
\!= | Inequality | U24, I24, F24 | U24 | \!= | Inequality | U24, I24, F24 | U24 |
\< | Less than | U24, I24, F24 | U24 | \< | Less than | U24, I24, F24 | U24 |
\<= | Less than or equal to | U24, I24, F24 | U24 | \<= | Less than or equal to | U24, I24, F24 | U24 |
\> | Greater than | U24, I24, F24 | U24 | \> | Greater than | U24, I24, F24 | U24 |
\>= | Greater than or equal to | U24, I24, F24 | U24 | \>= | Greater than or equal to | U24, I24, F24 | U24 |
\& | Bitwise and | U24, I24 | Same as arguments | \& | Bitwise and | U24, I24 | Same as arguments |
\| | Bitwise or | U24, I24 | Same as arguments | \| | Bitwise or | U24, I24 | Same as arguments |
\^ | Bitwise xor | U24, I24 | Same as arguments | \^ | Bitwise xor | U24, I24 | Same as arguments |
\** | Exponentiation | F24 | F24 | \*\* | 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 ### 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: Using everything we learned, we can write a program that calculates the n-th Fibonacci number using native numbers:
```py ```py
@ -140,7 +141,6 @@ fibonacci = λn # n is the argument
main = (fibonacci 15) main = (fibonacci 15)
``` ```
### Pattern matching numbers in Fun syntax equations ### Pattern matching numbers in Fun syntax equations
In Fun syntax, we can also use pattern matching equations to match on native unsigned numbers. In Fun syntax, we can also use pattern matching equations to match on native unsigned numbers.

View File

@ -108,4 +108,12 @@ 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_arg (defer_arg (defer_arg (defer @arg1 @arg2 @arg3 (f arg1 arg2 arg3)) arg1) arg2) arg3)
defer val = @x (x val) defer val = @x (x val)
defer_arg defered arg = @x (defered x arg) defer_arg defered arg = @x (defered x arg)
undefer defered = (defered @x x) 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)

View File

@ -15,8 +15,8 @@ main = (List/expand
(!= 20 10) (!= 20 10)
(< 20 10) (< 20 10)
(> 20 10) (> 20 10)
#(<< 10 2) (<< 10 2)
#(>> 10 2) (>> 10 2)
0xFFFF 0xFFFF
@ -77,7 +77,7 @@ main = (List/expand
(!= -20 +10) (!= -20 +10)
(< -20 +10) (< -20 +10)
(> -20 +10) (> -20 +10)
0xFFFF 0xFFFF
(+ +20.0 +10.0) (+ +20.0 +10.0)
@ -137,5 +137,10 @@ main = (List/expand
(!= -20.0 +10.0) (!= -20.0 +10.0)
(< -20.0 +10.0) (< -20.0 +10.0)
(> -20.0 +10.0) (> -20.0 +10.0)
0xFFFF
(log 2.0 3.0)
(atan2 3.0 4.0)
] ]
) )

View File

@ -3,7 +3,7 @@ source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/basic_num_ops.bend input_file: tests/golden_tests/run_file/basic_num_ops.bend
--- ---
NumScott: 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: 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]