<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 10. Jun 2017, at 03:18, Xiaodi Wu via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi all,<div class=""><br class=""></div><div class="">A few weeks ago, Joe Groff challenged users on Twitter to create a Rational&lt;T&gt; 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.</div><div class=""><br class=""></div><div class="">NumericAnnex &lt;<a href="https://github.com/xwu/NumericAnnex" class="">https://github.com/xwu/NumericAnnex</a>&gt; is meant to supplement the standard library's numerics facilities in Swift 4. At the moment, it provides:</div><div class=""><br class=""></div><div class="">* Extensions to BinaryInteger for GCD, LCM, and exponentiation</div><div class=""><br class=""></div><div class="">* Protocols Math (refines SignedNumeric), Real (refines Math and FloatingPoint), and PRNG &nbsp;(refines Sequence and IteratorProtocol)</div><div class=""><br class=""></div><div class="">* Types&nbsp;Complex&lt;T&gt;, Rational&lt;T&gt;, and Random</div><div class=""><br class=""></div><div class="">Documentation is available at &lt;<a href="https://xwu.github.io/NumericAnnex/" class="">https://xwu.github.io/NumericAnnex/</a>&gt;.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""><div class="">It looks good.&nbsp;</div><div class=""><br class=""></div><div class="">When it comes to trig functions, personally I’ve found it helpful to introduce an “Angle&lt;T&gt;” type to my own projects, rather than documenting “this angle expects radians” and having users do the conversion.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Angle&lt;T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">FloatingPoint</span><span style="font-variant-ligatures: no-common-ligatures" class="">&gt; {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> degrees(</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> radians(</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> degrees: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">degrees</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> degs): </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> degs</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">radians</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> rads): </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (rads / .</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">pi</span><span style="font-variant-ligatures: no-common-ligatures" class="">) * </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">180</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; }</span></div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp;&nbsp; &nbsp;</span></p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> radians: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">degrees</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> degs): </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (degs / </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">180</span><span style="font-variant-ligatures: no-common-ligatures" class="">) * .</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">pi</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">radians</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> rads): </span><span style="font-variant-ligatures: no-common-ligatures; color: #323e7d" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> rads</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; }</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Allows usage such as:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><font face="Menlo" class="">func rotate(by angle: Angle&lt;CGFloat&gt;) {&nbsp;</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;let rads = angle.radians</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;...</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">rotate(by: .degrees(90))</font></div><div class=""><font face="Menlo" class="">rotate(by: .radians(.pi/2))</font></div></blockquote><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div></body></html>