Created
July 20, 2014 08:53
-
-
Save amosr/54eef19ce63708107cfc to your computer and use it in GitHub Desktop.
I'm trying to write a Complex type that works for any floaty thing, but can't really get it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation; | |
protocol Num { | |
typealias T; | |
class func plus(T, T) -> T; | |
class func mul(T, T) -> T; | |
class func negate(T) -> T; | |
class func sub(T, T) -> T; | |
} | |
protocol Floating : Num { | |
//typealias T; | |
class func pi() -> T; | |
class func sin(_ : T) -> T; | |
class func cos(_ : T) -> T; | |
class func sqrt(_ : T) -> T; | |
} | |
func Fcos<A : Floating>(a : A.T) -> A.T { | |
return A.cos(a); | |
} | |
func Fsin<A : Floating>(a : A.T) -> A.T { | |
return A.sin(a); | |
} | |
func Fpi<A : Floating>() -> A.T { | |
return A.pi(); | |
} | |
func Fsqrt<A : Floating>(a : A.T) -> A.T { | |
return A.sqrt(a); | |
} | |
struct Complex<A : Floating> | |
{ | |
let re, im : A.T; | |
// cartesian | |
init(re:A.T, im:A.T) { | |
self.re = re; | |
self.im = im; | |
} | |
// polar | |
init(r : A.T, theta : A.T) { | |
// Cannot convert the expression's type from "A.T" to "A.T" | |
let e : A.T = Fcos(r); | |
// Cannot explicitly specialize a generic function | |
let e2 : A.T = Fcos<A>(r); | |
// ... | |
self.re = A.mul(r, A.cos(theta)); | |
self.im = A.mul(r, A.sin(theta)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment