Skip to content

Commit

Permalink
Merge pull request #585 from HigherOrderCO/583-add-log-and-atan2-oper…
Browse files Browse the repository at this point in the history
…ations-for-floats

#583 Add log and atan2 operations for floats
Nicolas Abril authored Jun 13, 2024
2 parents 4db1e68 + f11af64 commit 4d43bd9
Showing 5 changed files with 51 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
52 changes: 26 additions & 26 deletions docs/native-numbers.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 9 additions & 1 deletion src/fun/builtins.bend
Original file line number Diff line number Diff line change
@@ -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 val = @x (x val)
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)
13 changes: 9 additions & 4 deletions tests/golden_tests/run_file/basic_num_ops.bend
Original file line number Diff line number Diff line change
@@ -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)
]
)
)
4 changes: 2 additions & 2 deletions tests/snapshots/run_file__basic_num_ops.bend.snap
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 4d43bd9

Please sign in to comment.