[swift-evolution] [Pitch] Add the sign method to the SignedNumberType protocol.
Adam Nemecek
adamnemecek at gmail.com
Mon May 23 03:33:44 CDT 2016
Oh and also machine learning. Search for sign or signum and machine
learning, you'll find a lot of results.
On Mon, May 23, 2016 at 1:26 AM, Adam Nemecek <adamnemecek at gmail.com> wrote:
> It's not for branching based on the value, it's for calculating
> mathematical functions with the sign retrieved from the value. So for the
> same reason, no it should not be an enum. It should be the same type as the
> type it's called on. It's that way in Haskell as well
>
> http://hackage.haskell.org/package/base-4.9.0.0/docs/Prelude.html#v:signum
>
> This function is basically in every language
>
>
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
>
>
> https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#signum(double)
>
> http://en.cppreference.com/w/cpp/numeric/math/signbit
>
> https://golang.org/pkg/math/#Signbit
>
> https://msdn.microsoft.com/en-us/library/system.math.sign(v=vs.110).aspx
>
> It's used a bunch e.g. in dsp but also in mathematics
>
>
> https://www.quora.com/What-are-the-real-life-applications-of-Signum-function
>
> This quote somewhat summarizes it "So signum shows up in many places where
> discontinuous jumps must be written in closed form."
>
>
>
> On Mon, May 23, 2016 at 12:29 AM, Haravikk <swift-evolution at haravikk.me>
> wrote:
>
>> 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.
>>
>> In other words, anywhere that I might do this:
>>
>> if myValue.sign > 0 { … }
>>
>> I could just as easily do:
>>
>> if myValue > 0 { … }
>>
>> To the same end result surely? Unless I’m missing something it seems
>> redundant.
>>
>> 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?
>>
>> On 22 May 2016, at 08:07, Adam Nemecek via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>> Howdy,
>> 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.
>>
>> The implementation is fairly straight forward
>>
>> extension SignedNumberType {
>> var sign: Self {
>> if self == 0 {
>> return 0
>> }
>> else if self > 0 {
>> return 1
>> }
>> return -1
>> }
>> }
>>
>> 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.
>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160523/894c399b/attachment.html>
More information about the swift-evolution
mailing list