Numberick icon indicating copy to clipboard operation
Numberick copied to clipboard

Signed<Magnitude>

Open oscbyspro opened this issue 2 years ago • 10 comments

The final thing I want to investigate before committing to a flexible-width solution is whether I can efficiently model the signed part as a Signed<Magnitude> wrapper. I know fixed-width is possible, but that's different.

- NBKFlexibleWidthKit
    - UIntXL
    
- NBKSignedKit
    - SInt,   Signed<UInt>
    - SIntXL, Signed<UIntXL>

oscbyspro avatar Sep 14 '23 15:09 oscbyspro

Subquestion: if it's not possible, which set of changes would enable it?

oscbyspro avatar Sep 14 '23 17:09 oscbyspro

Making Signed<Magnitude> super light-weight, with no 2's complement and no fixed-width, is also an option.


More on this: I could introduce a non-binary NBKInteger protocol, that's like Swift.Numeric + division + words.

oscbyspro avatar Sep 15 '23 06:09 oscbyspro

I do love the light-weight approach. It's a high-value-low-maintenance coffee break project.

oscbyspro avatar Sep 15 '23 07:09 oscbyspro

I would need some composable magnitude text conversion methods, but that's easy to add.

oscbyspro avatar Sep 15 '23 07:09 oscbyspro

I'll need to make some changes, but it seems viable. I like it because it doesn't try to be something it is not, meaning it plays to its strengths and not to its weaknesses. It's much simpler that way and way easier to maintain. It also leaves a spot open for a two's complement solution, should I find the time or need it (#44).

oscbyspro avatar Sep 15 '23 17:09 oscbyspro

~~I just realized something neat. Since these modules all live within the same package, I can use #if canImport without adding any target dependencies.~~ Edit: never-mind, I was fooled by Xcode :disappointed:

// in NBKSignedKit/NBKSigned.swift
#if canImport(NBKFlexibleWidthKit)
import NBKFlexibleWidthKit
public typealias SIntXL = NBKSigned<UIntXL>
#endif

oscbyspro avatar Sep 16 '23 07:09 oscbyspro

I would need some composable magnitude text conversion methods, but that's easy to add.

Sign & magnitude encoding can be exposed in TextKit (#86), so I won't need another protocol method for that.

oscbyspro avatar Sep 24 '23 10:09 oscbyspro

Conforming this model to Strideable is a bit of a headache at the moment, so if that's desirable I might have to add some constraints. All core integers use Int strides, so maybe that's the way to go. Or Magnitude.Stride, perhaps?

oscbyspro avatar Oct 04 '23 11:10 oscbyspro

Hm. I kind of feel like this is teetering on the edge of over-engineering. Maybe I'll add a concrete sign-magnitude model to NBKFlexibleWidthKit and drop NBKSigned<Magnitude>. Dunno, I'll decide when I get to it.

oscbyspro avatar Nov 03 '23 13:11 oscbyspro

There's only two interesting specializations of it: UInt and UIntXL.

oscbyspro avatar Nov 03 '23 13:11 oscbyspro