From 9a1d25171690df1e83e3c8eb7187f6fc9a7d9698 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Mon, 11 Nov 2024 22:58:04 +0100 Subject: [PATCH] Fix issue with unsigned integers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/dlozeve/bqn-npy/issues/1. The spec says that •bit._cast should support 32-bit uints [1], but CBQN does not [2]. To get around this, we just use signed integers, which works for numbers smaller than 2^31. Above that, it will wrap around and (probably) fail, but CBQN does not support them anyway (except by converting them to doubles). From Marshall: > All of the provided functions at this time are identical on i32 and > u32, because they work (mod 2^32) and the only difference between > those formats is to add or subtract 2^32 from numbers outside the > range [0,2^31). > If you want to run on u32s stored directly as numbers in BQN (they > won't fit in i32 and will be stored as double!) you can •bit._cast > those into a signed format at the beginning and then back out at the > end. [1] https://mlochbaum.github.io/BQN/spec/system.html#bitwise-operations [2] https://matrix.to/#/!EjsgbQQNuTfHXQoiax:matrix.org/$PL2AYNlLQuYhPftdt7yw7rW9heiVEEeATte2zOt2BYk --- gentest.py | 12 +++++++++--- npy.bqn | 6 +++++- tests.bqn | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/gentest.py b/gentest.py index 118fae8..1fa2b0f 100644 --- a/gentest.py +++ b/gentest.py @@ -1,12 +1,18 @@ +# /// script +# dependencies = ["numpy"] +# /// + import numpy as np + def main(): for endianness in "<>": for typ in "fiu": for size in "48": - dtype=endianness + typ + size - arr = np.zeros((2,3,4), dtype=dtype) + dtype = endianness + typ + size + arr = np.zeros((2, 3, 4), dtype=dtype) np.save(f"test{dtype}.npy", arr) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/npy.bqn b/npy.bqn index 7763026..d3e997d 100644 --- a/npy.bqn +++ b/npy.bqn @@ -4,7 +4,11 @@ lf←@+10 magicString←(@+147)∾"NUMPY" # Type conversions -dtypes‿formats←⟨"