That’s a very neat helper!<br><br><br><div class="gmail_quote"><div dir="ltr">On Sat, Jun 10, 2017 at 10:12 Karl Wagner &lt;<a href="mailto:razielim@gmail.com">razielim@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On 10. Jun 2017, at 03:18, Xiaodi Wu via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_2965931359696920161Apple-interchange-newline"></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><div><div dir="ltr">Hi all,<div><br></div><div>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&#39;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&#39;d share it here in case anyone else might find it useful.</div><div><br></div><div>NumericAnnex &lt;<a href="https://github.com/xwu/NumericAnnex" target="_blank">https://github.com/xwu/NumericAnnex</a>&gt; is meant to supplement the standard library&#39;s numerics facilities in Swift 4. At the moment, it provides:</div><div><br></div><div>* Extensions to BinaryInteger for GCD, LCM, and exponentiation</div><div><br></div><div>* Protocols Math (refines SignedNumeric), Real (refines Math and FloatingPoint), and PRNG  (refines Sequence and IteratorProtocol)</div><div><br></div><div>* Types Complex&lt;T&gt;, Rational&lt;T&gt;, and Random</div><div><br></div><div>Documentation is available at &lt;<a href="https://xwu.github.io/NumericAnnex/" target="_blank">https://xwu.github.io/NumericAnnex/</a>&gt;.</div><div><br></div><div>I&#39;d love to hear feedback. I&#39;ll follow up shortly on Swift Evolution with some thoughts on future improvements to integer protocols based on this experience.</div><div><br></div></div></div></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><div>
_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br><div>It looks good. </div><div><br></div><div>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><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">enum</span><span style="font-variant-ligatures:no-common-ligatures"> Angle&lt;T: </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">FloatingPoint</span><span style="font-variant-ligatures:no-common-ligatures">&gt; {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">case</span><span style="font-variant-ligatures:no-common-ligatures"> degrees(</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">T</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">case</span><span style="font-variant-ligatures:no-common-ligatures"> radians(</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">T</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">var</span><span style="font-variant-ligatures:no-common-ligatures"> degrees: </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">T</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">switch</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">self</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">degrees</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">let</span><span style="font-variant-ligatures:no-common-ligatures"> degs): </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">return</span><span style="font-variant-ligatures:no-common-ligatures"> degs</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">radians</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">let</span><span style="font-variant-ligatures:no-common-ligatures"> rads): </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">return</span><span style="font-variant-ligatures:no-common-ligatures"> (rads / .</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">pi</span><span style="font-variant-ligatures:no-common-ligatures">) * </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">180</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">var</span><span style="font-variant-ligatures:no-common-ligatures"> radians: </span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">T</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">switch</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">self</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">degrees</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">let</span><span style="font-variant-ligatures:no-common-ligatures"> degs): </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">return</span><span style="font-variant-ligatures:no-common-ligatures"> (degs / </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">180</span><span style="font-variant-ligatures:no-common-ligatures">) * .</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">pi</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#587ea8">radians</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">let</span><span style="font-variant-ligatures:no-common-ligatures"> rads): </span><span style="font-variant-ligatures:no-common-ligatures;color:#323e7d">return</span><span style="font-variant-ligatures:no-common-ligatures"> rads</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">Allows usage such as:</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><font face="Menlo">func rotate(by angle: Angle&lt;CGFloat&gt;) { </font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">   let rads = angle.radians</font></div><div><font face="Menlo">   ...</font></div><div><font face="Menlo">}</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">rotate(by: .degrees(90))</font></div><div><font face="Menlo">rotate(by: .radians(.pi/2))</font></div></blockquote><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span></div></div></div></blockquote></div>