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

Finite<T> and Natural<T> trusted inputs #11

Closed
oscbyspro opened this issue Jun 9, 2024 · 0 comments
Closed

Finite<T> and Natural<T> trusted inputs #11

oscbyspro opened this issue Jun 9, 2024 · 0 comments
Labels
addition oh, so shiny!

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Jun 9, 2024

I have settled on hoisting the GCD and XGCD preconditions (#9) via the Trusted Input ™️ pattern to enable recovery. As an example, the XGCD algorithms should extend all unsigned integers and they only fail when the arguments are infinite. I cannot limit these algorithms to a finite integer types because that makes them unavailable to InfiniInt<T>. So, instead, I'll just require that the inputs are of the Finite<T> type:

extension BinaryInteger {
    @inlinable public static func euclidean(
        _ lhs: consuming Finite<Self>,
        _ rhs: consuming Finite<Self>
    ) -> Magnitude
    
    @inlinable public static func euclidean1(
	_ lhs: consuming Finite<Self>,
	_ rhs: consuming Finite<Self>
    ) -> XGCD1 where Self: UnsignedInteger
    
    @inlinable public static func euclidean2(
	_ lhs: consuming Finite<Self>,
	_ rhs: consuming Finite<Self>
    ) -> XGCD2 where Self: UnsignedInteger
}

Some convenience methods can then be written as finite integer extensions:

extension FiniteInteger {
    @inlinable public consuming func euclidean(_ other: consuming Self) -> Magnitude {
        Self.euclidean (Finite(unchecked: self), Finite(unchecked: other))
    }

    @inlinable public consuming func euclidean1(_ other: consuming Self) -> XGCD1 where Self: UnsignedInteger {
        Self.euclidean1(Finite(unchecked: self), Finite(unchecked: other))
    }

    @inlinable public consuming func euclidean2(_ other: consuming Self) -> XGCD2 where Self: UnsignedInteger {
        Self.euclidean2(Finite(unchecked: self), Finite(unchecked: other))
    }
}

The current use case does not need Natural<T> but I imagine that it is of similar importance.

@oscbyspro oscbyspro added the addition oh, so shiny! label Jun 9, 2024
@oscbyspro oscbyspro added this to the Ultimathnum 0.4.0 milestone Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition oh, so shiny!
Projects
None yet
Development

No branches or pull requests

1 participant