# A protocol-oriented numerics library

(Xiaodi Wu) #1

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> 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/>.

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.

(Karl) #2

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)

public var degrees: T {
switch self {
case .degrees(let degs): return degs
}
}

switch self {
case .degrees(let degs): return (degs / 180) * .pi
}
}
}

Allows usage such as:

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

...
}

rotate(by: .degrees(90))

···

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> 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/>.

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

(Xiaodi Wu) #3

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> 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/>.

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)

public var degrees: T {
switch self {
case .degrees(let degs): return degs
}
}

switch self {
case .degrees(let degs): return (degs / 180) * .pi
}
}
}

Allows usage such as:

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