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:
@@ -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
|
||||
|
Reference in New Issue
Block a user