<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=""><div class="">Could you give an example of this method’s usage? Surely your value is either positive, negative or zero already, so this method doesn’t return anything more useful.</div><div class=""><br class=""></div><div class="">In other words, anywhere that I might do this:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if myValue.sign > 0 { … }</font></div><div class=""><br class=""></div><div class="">I could just as easily do:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if myValue > 0 { … }</font></div><div class=""><br class=""></div><div class="">To the same end result surely? Unless I’m missing something it seems redundant.</div><div class=""><br class=""></div><div class="">If there is a use-case for this, would it make more sense to have the return type as an enum with cases for Positive, Negative and Zero?</div><br class=""><div><blockquote type="cite" class=""><div class="">On 22 May 2016, at 08:07, Adam Nemecek via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</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=""> var sign: Self {</div><div class=""> if self == 0 {</div><div class=""> return 0</div><div class=""> }</div><div class=""> else if self > 0 {</div><div class=""> return 1</div><div class=""> }</div><div class=""> return -1</div><div class=""> }</div><div class="">} </div><div class=""><br class=""></div><div class="">I was trying to implement is without branching by doing (x > 0) - (x < 0) but I couldn't get the types right so I'm open to suggestions.</div><div class=""><br class=""></div><div class=""><br class=""></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>