oscbyspro / Ultimathnum

Binary arithmetic reimagined in Swift
Apache License 2.0
9 stars 1 forks source link

More arithmetic conveniences #119

Closed oscbyspro closed 3 weeks ago

oscbyspro commented 3 weeks ago

Now that the most generic division result is optional (#69) (#95), it makes sense to add conveniences mapping division by zero to nil. Additionally, I can add non-optional and non-fallible conveniences in the ArbitraryInteger & SignedInteger (lenient) case like I've already done in the SystemsInteger & UnsignedInteger (natural) case.

extension BinaryInteger {
    func quotient (_ divisor: Self) -> Optional<Fallible<Self>>
    func remainder(_ divisor: Self) -> Optional<Self>
    func division (_ divisor: Self) -> Optional<Fallible<Division<Self, Self>>>
}

extension BinaryInteger where Self: ArbitraryInteger & SignedInteger {
    func quotient (_ divisor: Self) -> Optional<Self> 
    func division (_ divisor: Self) -> Optional<Division<Self, Self>> 
    func quotient (_ divisor: Nonzero<Self>) -> Self
    func division (_ divisor: Nonzero<Self>) -> Division<Self, Self> 
}

extension BinaryInteger where Self: SystemsInteger & UnsignedInteger {
    func quotient (_ divisor: Self) -> Optional<Self>
    func division (_ divisor: Self) -> Optional<Division<Self, Self>>
}
oscbyspro commented 3 weeks ago

ArbitraryInteger & SignedInteger multiplication is never lossy:

extension BinaryInteger where Self: ArbitraryInteger & SignedInteger {
    func squared() -> Self 
    func times(_ other: Self) -> Self
}
oscbyspro commented 3 weeks ago

ArbitraryInteger & SignedInteger addition is never lossy:

extension BinaryInteger where Self: ArbitraryInteger & SignedInteger {
    mutating func negate()
    func negated() ->   Self
    func plus (_ other: Self) ->  Self
    func minus(_ other: Self) ->  Self
    func incremented(_ condition: Bool = true) -> Self
    func decremented(_ condition: Bool = true) -> Self
}
oscbyspro commented 2 weeks ago

ArbitraryInteger & SignedInteger factorial is never lossy:

extension BinaryInteger where Self: ArbitraryInteger & SignedInteger {
    func factorial() -> Optional<Self>
}