1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-23 03:37:40 +00:00

add int/to-number: converts s64 and u64 to numbers

(int/to-number value) converts an s64 or u64 to a number.
It restricts the value to the int32 range,
so that `int32?` will always suceeded when called on the result.
This commit is contained in:
Ian Shehadeh
2022-02-20 16:16:52 -05:00
parent 6f645c4cb7
commit 88db9751d7
2 changed files with 47 additions and 0 deletions

View File

@@ -39,6 +39,20 @@
(def c (u64 "32rvv_vv_vv_vv"))
(def d (u64 "123456789"))))
# Conversion back to an int32
(assert (= (int/to-number (u64 0xFaFa)) 0xFaFa))
(assert (= (int/to-number (i64 0xFaFa)) 0xFaFa))
(assert-error
"int64 out of bounds for int32"
(do
(int/to-number (u64 "0x7fff_ffff_ffff_ffff"))
(int/to-number (i64 "-0x7fff_ffff_ffff_ffff"))))
(assert-error
"int/to-number fails on non-abstract types"
(int/to-number 1))
(assert-no-error
"create some int64 bigints"
(do