Add native number cast operations

This commit is contained in:
Nicolas Abril 2024-06-15 20:51:11 +02:00
parent f8d6d13a24
commit ba8561c3d4
7 changed files with 98 additions and 6 deletions

View File

@ -18,8 +18,9 @@ and this project does not currently adhere to a particular versioning scheme.
### Added
- Add `log` and `atan2` builtin functions. ([#583][gh-583])
- Create new type of top-level definition for writing native HVM definitions. ([#586][gh-586])
- Add `log` and `atan2` builtin functions. ([#583][gh-583])
- Add `to_f24`, `to_u24` and `to_i24` number casting builtin functions. ([#582][gh-582])
## [0.2.35] - 2024-06-06
@ -340,6 +341,7 @@ 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-582]: https://github.com/HigherOrderCO/Bend/issues/582
[gh-583]: https://github.com/HigherOrderCO/Bend/issues/583
[gh-586]: https://github.com/HigherOrderCO/Bend/issues/586
[Unreleased]: https://github.com/HigherOrderCO/Bend/compare/0.2.35...HEAD

View File

@ -242,3 +242,21 @@ def atan2(x: f24, y: f24) -> f24
Computes the arctangent of `y / x`.
Has the same behaviour as `atan2f` in the C math lib.
### to_f24
```py
def to_f24(x: any number) -> f24
```
Casts any native number to an f24.
### to_u24
```py
def to_u24(x: any number) -> u24
```
Casts any native number to a u24.
### to_i24
```py
def to_i24(x: any number) -> i24
```
Casts any native number to an i24.

View File

@ -126,3 +126,18 @@ hvm log:
# Computes the arctangent of the quotient of its two arguments.
hvm atan2:
($([&] $(y ret)) (y ret))
# to_f24(x: native number) -> f24
# Casts any native number to an f24.
hvm to_f24:
($([f24] ret) ret)
# to_u24(x: native number) -> u24
# Casts any native number to a u24.
hvm to_u24:
($([u24] ret) ret)
# to_i24(x: native number) -> i24
# Casts any native number to an i24.
hvm to_i24:
($([i24] ret) ret)

View File

@ -129,21 +129,21 @@ pub fn desugar_book(
ctx.book.make_var_names_unique();
ctx.book.desugar_use();
ctx.book.make_var_names_unique();
ctx.book.linearize_vars();
// sanity check
ctx.check_unbound_vars()?;
// Optimizing passes
if opts.float_combinators {
ctx.book.float_combinators(MAX_NET_SIZE);
}
// sanity check
ctx.check_unbound_refs()?;
// Optimizing passes
ctx.prune(opts.prune);
if opts.merge {
ctx.book.merge_definitions();
}

View File

@ -1,5 +1,5 @@
# Expected +42.0
hvm to_f24:
hvm to_f24_:
($([f24] a) a)
main = (to_f24 42)
main = (to_f24_ 42)

View File

@ -0,0 +1,48 @@
main =
use inf = (** 9.0 (** 9.0 9.0))
(
(to_u24 0),
(to_u24 1),
(to_u24 123456),
(to_u24 -1),
(to_u24 +1),
(to_u24 +0),
(to_u24 +400),
(to_u24 1.0),
(to_u24 1.5),
(to_u24 -3.0),
(to_u24 -3.5),
(to_u24 inf), # inf
(to_u24 (* -1.0 inf)), # -inf
(to_u24 (/ inf inf)), # nan (inf/inf)
99999,
(to_i24 0),
(to_i24 1),
(to_i24 123456),
(to_i24 -1),
(to_i24 +1),
(to_i24 +0),
(to_i24 +400),
(to_i24 1.0),
(to_i24 1.5),
(to_i24 -3.0),
(to_i24 -3.5),
(to_i24 inf), # inf
(to_i24 (* -1.0 inf)), # -inf
(to_i24 (/ inf inf)), # nan (inf/inf)
99999,
(to_f24 0),
(to_f24 1),
(to_f24 123456),
(to_f24 -1),
(to_f24 +1),
(to_f24 +0),
(to_f24 +400),
(to_f24 1.0),
(to_f24 1.5),
(to_f24 -3.0),
(to_f24 -3.5),
(to_f24 inf), # inf
(to_f24 (* -1.0 inf)), # -inf
(to_f24 (/ inf inf)) # nan (inf/inf)
)

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/num_cast.bend
---
NumScott:
(0, (1, (123456, (16777215, (1, (0, (400, (1, (1, (0, (0, (16777215, (0, (0, (99999, (+0, (+1, (+123456, (-1, (+1, (+0, (+400, (+1, (+1, (-3, (-3, (+8388607, (-8388608, (+0, (99999, (0.000, (1.000, (123456.000, (-1.000, (1.000, (0.000, (400.000, (1.000, (1.500, (-3.000, (-3.500, (inf, (-inf, NaN)))))))))))))))))))))))))))))))))))))))))))
Scott:
(0, (1, (123456, (16777215, (1, (0, (400, (1, (1, (0, (0, (16777215, (0, (0, (99999, (+0, (+1, (+123456, (-1, (+1, (+0, (+400, (+1, (+1, (-3, (-3, (+8388607, (-8388608, (+0, (99999, (0.000, (1.000, (123456.000, (-1.000, (1.000, (0.000, (400.000, (1.000, (1.500, (-3.000, (-3.500, (inf, (-inf, NaN)))))))))))))))))))))))))))))))))))))))))))