Missing initializers from/to FloatingPointType


(Dan Raviv) #1

While FloatingPointType can be initialized from various Int type variants,
it seems to be missing an initializer from Double/Float. Similarly, there
are no initializers from FloatingPointType to Double/Float. Is this
intentional?

I've tried implementing the following:

// https://en.wikipedia.org/wiki/Window_function#Tukey_window
public func tukeyWindowFunc(index: Int, N: Int) -> Double {
    let L = N/8
    let indexAbs = min(index, N-1-index)
    if indexAbs >= L {
        return 1.0
    }
    else {
        let r = Double(indexAbs) / Double(L)
        return 0.5*(1.0 + cos(M_PI * (r - 1.0)))
    }
}

extension Array where Element: FloatingPointType {
    public func tukeyWindowArray() -> [Element] {
        return (0..<count).map{self[$0] * tukeyWindowFunc($0, N: count)}
    }
}

The extension failed to compile no matter how I spun it, since Double and
FloatingPointType can't multiply each other and neither type can be cast to
the other.

I would settle for extending just Array<Double>, but that isn't supported
yet either :wink:

Cheers,
Dan


(Max Moiseev) #2

Hi Dan,

New protocols are in the making for both integers and floating point numbers.

You particular problem is addressed here: https://github.com/stephentyrone/swift/commits/floating-point-revision

max

···

On Mar 30, 2016, at 6:36 AM, Dan Raviv via swift-evolution <swift-evolution@swift.org> wrote:

While FloatingPointType can be initialized from various Int type variants, it seems to be missing an initializer from Double/Float. Similarly, there are no initializers from FloatingPointType to Double/Float. Is this intentional?

I've tried implementing the following:

// https://en.wikipedia.org/wiki/Window_function#Tukey_window
public func tukeyWindowFunc(index: Int, N: Int) -> Double {
    let L = N/8
    let indexAbs = min(index, N-1-index)
    if indexAbs >= L {
        return 1.0
    }
    else {
        let r = Double(indexAbs) / Double(L)
        return 0.5*(1.0 + cos(M_PI * (r - 1.0)))
    }
}

extension Array where Element: FloatingPointType {
    public func tukeyWindowArray() -> [Element] {
        return (0..<count).map{self[$0] * tukeyWindowFunc($0, N: count)}
    }
}

The extension failed to compile no matter how I spun it, since Double and FloatingPointType can't multiply each other and neither type can be cast to the other.

I would settle for extending just Array<Double>, but that isn't supported yet either :wink:

Cheers,
Dan
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Stephen Canon) #3

The draft proposal may be easier reading:

https://github.com/stephentyrone/swift-evolution/blob/master/NNNN-floating-point-protocols.md

···

On Apr 7, 2016, at 11:16 AM, Max Moiseev via swift-evolution <swift-evolution@swift.org> wrote:

Hi Dan,

New protocols are in the making for both integers and floating point numbers.

You particular problem is addressed here: https://github.com/stephentyrone/swift/commits/floating-point-revision

max

On Mar 30, 2016, at 6:36 AM, Dan Raviv via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

While FloatingPointType can be initialized from various Int type variants, it seems to be missing an initializer from Double/Float. Similarly, there are no initializers from FloatingPointType to Double/Float. Is this intentional?

I've tried implementing the following:

// https://en.wikipedia.org/wiki/Window_function#Tukey_window
public func tukeyWindowFunc(index: Int, N: Int) -> Double {
    let L = N/8
    let indexAbs = min(index, N-1-index)
    if indexAbs >= L {
        return 1.0
    }
    else {
        let r = Double(indexAbs) / Double(L)
        return 0.5*(1.0 + cos(M_PI * (r - 1.0)))
    }
}

extension Array where Element: FloatingPointType {
    public func tukeyWindowArray() -> [Element] {
        return (0..<count).map{self[$0] * tukeyWindowFunc($0, N: count)}
    }
}

The extension failed to compile no matter how I spun it, since Double and FloatingPointType can't multiply each other and neither type can be cast to the other.

I would settle for extending just Array<Double>, but that isn't supported yet either :wink:

Cheers,
Dan
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution