A protocol-oriented numerics library

Hi all,

A few weeks ago, Joe Groff challenged users on Twitter to create a
Rational<T> type making use of the new integer protocols implemented for
Swift 4. As it happens, I'd already started a project to try out these new
number protocols. Now that I think the most embarrassing bugs have been
squashed in that project, I thought I'd share it here in case anyone else
might find it useful.

NumericAnnex <https://github.com/xwu/NumericAnnex&gt; is meant to supplement
the standard library's numerics facilities in Swift 4. At the moment, it
provides:

* Extensions to BinaryInteger for GCD, LCM, and exponentiation

* Protocols Math (refines SignedNumeric), Real (refines Math and
FloatingPoint), and PRNG (refines Sequence and IteratorProtocol)

* Types Complex<T>, Rational<T>, and Random

Documentation is available at <https://xwu.github.io/NumericAnnex/&gt;\.

I'd love to hear feedback. I'll follow up shortly on Swift Evolution with
some thoughts on future improvements to integer protocols based on this
experience.

It looks good.

When it comes to trig functions, personally I’ve found it helpful to introduce an “Angle<T>” type to my own projects, rather than documenting “this angle expects radians” and having users do the conversion.

public enum Angle<T: FloatingPoint> {
    case degrees(T)
    case radians(T)

    public var degrees: T {
        switch self {
        case .degrees(let degs): return degs
        case .radians(let rads): return (rads / .pi) * 180
        }
    }
    
    public var radians: T {
        switch self {
        case .degrees(let degs): return (degs / 180) * .pi
        case .radians(let rads): return rads
        }
    }
}

Allows usage such as:

func rotate(by angle: Angle<CGFloat>) {

   let rads = angle.radians
   ...
}

rotate(by: .degrees(90))
rotate(by: .radians(.pi/2))

···

On 10. Jun 2017, at 03:18, Xiaodi Wu via swift-users <swift-users@swift.org> wrote:

Hi all,

A few weeks ago, Joe Groff challenged users on Twitter to create a Rational<T> type making use of the new integer protocols implemented for Swift 4. As it happens, I'd already started a project to try out these new number protocols. Now that I think the most embarrassing bugs have been squashed in that project, I thought I'd share it here in case anyone else might find it useful.

NumericAnnex <https://github.com/xwu/NumericAnnex&gt; is meant to supplement the standard library's numerics facilities in Swift 4. At the moment, it provides:

* Extensions to BinaryInteger for GCD, LCM, and exponentiation

* Protocols Math (refines SignedNumeric), Real (refines Math and FloatingPoint), and PRNG (refines Sequence and IteratorProtocol)

* Types Complex<T>, Rational<T>, and Random

Documentation is available at <https://xwu.github.io/NumericAnnex/&gt;\.

I'd love to hear feedback. I'll follow up shortly on Swift Evolution with some thoughts on future improvements to integer protocols based on this experience.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

That’s a very neat helper!

···

On Sat, Jun 10, 2017 at 10:12 Karl Wagner <razielim@gmail.com> wrote:

On 10. Jun 2017, at 03:18, Xiaodi Wu via swift-users < > swift-users@swift.org> wrote:

Hi all,

A few weeks ago, Joe Groff challenged users on Twitter to create a
Rational<T> type making use of the new integer protocols implemented for
Swift 4. As it happens, I'd already started a project to try out these new
number protocols. Now that I think the most embarrassing bugs have been
squashed in that project, I thought I'd share it here in case anyone else
might find it useful.

NumericAnnex <https://github.com/xwu/NumericAnnex&gt; is meant to supplement
the standard library's numerics facilities in Swift 4. At the moment, it
provides:

* Extensions to BinaryInteger for GCD, LCM, and exponentiation

* Protocols Math (refines SignedNumeric), Real (refines Math and
FloatingPoint), and PRNG (refines Sequence and IteratorProtocol)

* Types Complex<T>, Rational<T>, and Random

Documentation is available at <https://xwu.github.io/NumericAnnex/&gt;\.

I'd love to hear feedback. I'll follow up shortly on Swift Evolution with
some thoughts on future improvements to integer protocols based on this
experience.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

It looks good.

When it comes to trig functions, personally I’ve found it helpful to
introduce an “Angle<T>” type to my own projects, rather than documenting
“this angle expects radians” and having users do the conversion.

public enum Angle<T: FloatingPoint> {
    case degrees(T)
    case radians(T)

    public var degrees: T {
        switch self {
        case .degrees(let degs): return degs
        case .radians(let rads): return (rads / .pi) * 180
        }
    }

    public var radians: T {
        switch self {
        case .degrees(let degs): return (degs / 180) * .pi
        case .radians(let rads): return rads
        }
    }
}

Allows usage such as:

func rotate(by angle: Angle<CGFloat>) {

   let rads = angle.radians
   ...
}

rotate(by: .degrees(90))
rotate(by: .radians(.pi/2))