oscbyspro / Ultimathnum

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

BinaryInteger/isZero and DataInt/isZero #12

Closed oscbyspro closed 3 weeks ago

oscbyspro commented 4 weeks ago

I've changed my mind about BinaryInteger/isZero. It's common enough to deserves a seat among isInfinite and isNegative. Even integers with unknown signedness can implement it, so DataInt/isZero makes sense too. A binary integer extension can also ensure that big integers perform the operation in place, which is preferred for performance reasons.

extension BinaryInteger {

    /// Indicates whether this value is equal to zero.
    ///
    /// - Note: Big integers evaluate it in place, cf. `compared(to: 0)`.
    ///
    @inlinable public var isZero: Bool {
        if !Self.size.isInfinite {
            return self == Self.zero

        }   else {
            return self.withUnsafeBinaryIntegerElements {
                $0.isZero
            }
        }
    }
}
oscbyspro commented 4 weeks ago

InfiniInt\<T> already has an isZero getter and this extension on binary integer makes it obsolete.