Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Galaxy-brain exactly(sign:magnitude:) #9

Closed
oscbyspro opened this issue Jun 9, 2023 · 1 comment
Closed

Galaxy-brain exactly(sign:magnitude:) #9

oscbyspro opened this issue Jun 9, 2023 · 1 comment
Labels
brrr such code, much wow
Milestone

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Jun 9, 2023

While this API is still internal-ish, I'd like to note that formTwosComplementSubsequence(true) can replace the isZero check in exactly(sign:magnitude:). It's similar to (#6), hence the meme.

@inlinable public static func exactly<T>(sign: Sign, magnitude: T.Magnitude) -> T? where T: NBKFixedWidthInteger {
    var bitPattern = magnitude as T.Magnitude
    var isLessThanZero = (sign == Sign.minus)
    if  isLessThanZero {
        isLessThanZero = !bitPattern.formTwosComplementSubsequence(true)
    }
    
    let value = T(bitPattern: bitPattern)
    return value.isLessThanZero == isLessThanZero ? value : nil
}
@oscbyspro oscbyspro added the brrr such code, much wow label Jun 9, 2023
@oscbyspro oscbyspro added this to the v0.3.0 milestone Jun 9, 2023
@oscbyspro
Copy link
Owner Author

oscbyspro commented Jun 10, 2023

I had to check if minus zero fall under the definition of "exactly representable" and it does according to Swift:

let zero = Int(exactly: Double(sign: .minus, exponentBitPattern: 0, significandBitPattern: 0))

@oscbyspro oscbyspro changed the title Galaxy brain exactly(sign:magnitude:) Galaxy-brain exactly(sign:magnitude:) Jun 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
brrr such code, much wow
Projects
None yet
Development

No branches or pull requests

1 participant