Fast, math functions and protocols

I am trying to create 2 protocols ArithmeticType and MathematicType, which will be used in cases where sentences of common operator functions

protocol ArithmeticType {
    func +(lhs: Self, rhs: Self) -> Self
    func -(lhs: Self, rhs: Self) -> Self
    func *(lhs: Self, rhs: Self) -> Self
    func /(lhs: Self, rhs: Self) -> Self
}

extension Int : ArithmeticType {
}

extension Double : ArithmeticType {
}

extension Float : ArithmeticType {
}

ArithmeticType works as expected, and Int, Float, and Double match it. However, the following errors

import Darwin

protocol MathematicType {
    func sin(x: Self) -> Self
}

extension Double : MathematicType {
}

extension Float : MathematicType {
}

on the console output of the playground I read:

Playground execution failed: <EXPR>:35:1: error: type 'Double' does not conform to protocol 'MathematicType'
extension Double : MathematicType {
^
<EXPR>:32:10: note: protocol requires function 'sin' with type 'Double -> Self'
    func sin(x: Self) -> Self
         ^
<EXPR>:39:1: error: type 'Float' does not conform to protocol 'MathematicType'
extension Float : MathematicType {
^
<EXPR>:32:10: note: protocol requires function 'sin' with type 'Float -> Self'
    func sin(x: Self) -> Self
         ^

I would like mathematical functions to behave like the operators above. Is there any way?

== EDIT:

Now I understand that trying to simplify my question was a bad idea. context - this class (vector of optional values)

class Vector<T> {

    var data=[T?]()

    init(fromArray: Array<T>) {
        for i in fromArray {
            data.append(i)
        }
    }

    init() {
    }

    init(count: Int){
        for i in 0..<count {
            data.append(nil)
        }
    }

    init(count: Int, repeatedValue: T) {
        for i in 0..<count {
            data.append(repeatedValue)
        }
    }

    func count() -> Int {
        return data.count
    }

    func append(newElement: T?) {
        data.append(newElement)
    }

    subscript(index: Int) -> T? {
        let i = index>0 ? index % count() : -index % count()
        return data[i]
    }
}

outside of this, I defined a common function for the + operator

func +<T where T: ArithmeticType>(left: Vector<T>, right: Vector<T>) -> Vector<T> {
    let resultCount = max(left.count(),right.count())
    var result = Vector<T>()
    for i in 0..<resultCount {
        if left[i] != nil && right[i] != nil {
            result.append(left[i]!+right[i]!)
        }
        else {
            result.append(nil)
        }
    }
    return result
}

works as expected, however, when I tried to define the general sin function as

func sin<T where T : FloatingPointType>(x: Vector<T>) -> Vector<T>{
    var result = Vector<T>()
    for i in 0..<x.count() {
        if let o = x[i] {
            result.append(sin(o))
        }
        else {
            result.append(nil)
        }
    }
    return result
}

I got "could not find an overload of sin that accepts the arguments posed"

MathemticType , +

(ArithmeticType IntegerAritmeticType, , import swift , , )

== UPDATE

Double

func sin(x: Vector<Double>) -> Vector<Double>{
    var result = Vector<Double>()
    for i in 0..<x.count() {
        if let o = x[i] {
            result.append(Darwin.sin(o))
        }
        else {
            result.append(nil)
        }
    }
    return result
}

.

, : " Double, Float"?

+4
3

, sin() MathematicType, , Double MathematicType, sin().

extension Double {
    func sin(x: Double) -> Double {
        return Darwin.sin(x)
    }
}

, , ? :

let myAngle = 3.14159
let sineValue = myAngle.sin()

, :

protocol MathematicType {
    func sin() -> Self
}

extension Double : MathematicType {
    func sin() -> Double {
        return Darwin.sin(self)
    }
}
+2

MathematicType Float Double, , Float Double sin . , :

let zero = 0.0    // inferred type Double
zero.sin(1.5707963268) // returns about 1.0

, sin zero, , , , .

, , sin , :

sin(1.5707963268)

Right?

... :

func sin(x: Double) -> Double
func sin(x: Float) -> Float

, MathematicType , ", ", sin. - ( kludgy, , ):

func sine<T: FloatingPointType>(x: T) -> T {
    if let v = x as? Double {
        return sin(v) as T
    }
    if let v = x as? Float {
        return sin(v) as T
    }
    fatalError("unknown FloatingPointType")
}

( , , Float, Double, .)

+2

, , , . , . , - . .

+1

All Articles