Numberick icon indicating copy to clipboard operation
Numberick copied to clipboard

NBKFlexibleWidthKit

Open oscbyspro opened this issue 2 years ago • 12 comments

I'm working on a Big[U]Int™ module. That's it. That's the ~~tweet~~ GitHub post.

oscbyspro avatar Jul 12 '23 14:07 oscbyspro

I've pondered normalization behaviors for a bit, and I wonder if there's merit in a dynamically-fixed-width type. This hypothetical integer type would be given a size at initialization, then behave much like a fixed-width type unless specifically told to resize or whatnot. I also imagine that you could build a canonical big integer type on top of it. Something like NBKDynamicWidthKit and NBKAutomaticWidthKit, where NBKAutomaticWidthKit provides automatic normalization. Hm.

oscbyspro avatar Jul 15 '23 11:07 oscbyspro

2's complement or sign(um)-magnitude, that is the question :thought_balloon:

oscbyspro avatar Jul 26 '23 10:07 oscbyspro

I'm inclined to go with sign-magnitude because then I can leverage the magnitude. I wonder whether I should go sign or signum, however. Integer solutions become infinitely less elegant once you step out of the fixed-width real. A sign is either asymmetrically normalized or produces two zero representations. A signum, on the other hand, duplicates information that is trivially derived by checking whether the magnitude is zero or not. Hm.

oscbyspro avatar Jul 27 '23 09:07 oscbyspro

One nice thing about allowing both positive and negative zero, however, is that you can manipulate the sign and magnitude independently; there's one less invariant to maintain.

oscbyspro avatar Jul 28 '23 09:07 oscbyspro

On a side note: I believe a two's complement in-memory solution is easier to implement because the as-if-two's-complement behavior is the biggest headache by far. I'm just not convinced I could make it as performant, because negating arbitrary two's complement is quite expensive compared to toggling a sign bit.


Thought: I wonder if you can mitigate the cost with a lazy magnitude view 💭

oscbyspro avatar Jul 28 '23 09:07 oscbyspro

I decided to go with sign-magnitude and embrace negative zero :hugs:

oscbyspro avatar Jul 28 '23 11:07 oscbyspro

I'm thinking of adding a NBKResizableWidthKit (#46), even if it's only to house some kind of resizable-but-never-empty collection of words. I don't really want juggle two sets of invariants in-line (#47). Hm.

oscbyspro avatar Aug 04 '23 13:08 oscbyspro

Untangling NBKResizableWidthKit (#46) might take a while. Instead of a fixed-width-but-resizable storage model, I might opt for freestanding generic functions. That way the logic can be reused on buffer pointers, etc. I will see what works out.

oscbyspro avatar Sep 11 '23 05:09 oscbyspro

UIntXL is close to done, but Signed<UIntXL> still needs some work. Maybe I should split NBKSignedKit (#81) into a different feature branch? I have developed them side-by-side because the latter needs proper integer models for testing. Hm.

oscbyspro avatar Oct 22 '23 06:10 oscbyspro

I have put a lot of effort into separating my models and algorithms. So NBKFlexibleWidthKit mostly handles branching, resizing and normalization logic, while NBKCoreKit takes care of the calculations. I find this approach easy to maintain and reason about. (#85) helps a lot.

oscbyspro avatar Oct 22 '23 07:10 oscbyspro

Given that auto-normalization is the main feature of these integers, NBKNormalWidthKit is perhaps a more apt name.

oscbyspro avatar Dec 10 '23 10:12 oscbyspro

Hm. I wonder if I should make it UIntXL generic over its element type...

oscbyspro avatar Dec 10 '23 10:12 oscbyspro