<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="">Le 22 mai 2016 à 03:07, Adam Nemecek via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Howdy,</div><div class="">I think that the SignedNumberType should implement a method called sign that will return -1 for negative numbers, 0 for 0 and 1 for positive numbers. This is similar to the signum method in e.g. Java and similarly called methods in other languages.</div><div class=""><br class=""></div><div class="">The implementation is fairly straight forward</div><div class=""><br class=""></div><div class="">extension SignedNumberType {</div><div class="">&nbsp; var sign: Self {</div><div class="">&nbsp; &nbsp; if self == 0 {</div><div class="">&nbsp; &nbsp; &nbsp; return 0</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; else if self &gt; 0 {</div><div class="">&nbsp; &nbsp; &nbsp; return 1</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; return -1</div><div class="">&nbsp; }</div><div class="">}&nbsp;</div><div class=""><br class=""></div><div class="">I was trying to implement is without branching by doing (x &gt; 0) - (x &lt; 0) but I couldn't get the types right so I'm open to suggestions.</div><div class=""><br class=""></div></div></div></blockquote><br class=""></div><div>Challenge accepted… Removed the if/else if, at the cost of double function call to a tri-op:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">extension<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> as01&lt;T:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">SignedNumberType</span>&gt;() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> ? <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> : <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>SignedNumberType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> sign: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">&gt;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>).<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">as01</span>() <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">-</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">&lt;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>).<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">as01</span>() }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Dany</div></div></body></html>