[swift-users] Difficulties extending BinaryFloatingPoint

Jens Persson jens at bitcycle.com
Thu Jan 5 07:22:22 CST 2017


The code below doesn't compile since there is no exponential function (exp)
that works on all FloatingPoint or BinaryFloatingPoint types, also no
protocol seems to define the power function or the constant e, although
they do define for example: basic arithmetic operators, squareRoot() and pi.

extension BinaryFloatingPoint {
    func sigmoid() -> Self {
        return 1.0 / (1.0 + exp(-self))
    }
}

I could, but don't want to write two free funcs sigmoid(Float) -> Float and
sigmoid(Double) -> Double, because I need to use x.sigmoid() in several
places where x is of a generic type (a BinaryFloatingPoint).

More generally: I would have the same problem if I needed eg sin or cos.
Are there any particular reason why, given a generic BinaryFloatingPoint, I
cannot use sin or cos, while I can use pi and squareRoot()? It does seem a
bit arbitrary.

Any ideas on how to implement a sigmoid() that works for all
BinaryFloatingPoint types?

/Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170105/d0dba2ad/attachment.html>


More information about the swift-users mailing list