- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathcomplex.swift
21 lines (19 loc) · 610 Bytes
/
complex.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// RUN: %target-typecheck-verify-swift
publicstructComplex{
publicvarreal=0.0,imag=0.0
publicfunc magnitude()->Double{
return real * real + imag * imag
}
publicinit(){}
publicinit(real:Double, imag:Double){
self.real = real
self.imag = imag
}
}
publicfunc*(lhs:Complex, rhs:Complex)->Complex{
returnComplex(real: lhs.real * rhs.real - lhs.imag * rhs.imag,
imag: lhs.real * rhs.imag + lhs.imag * rhs.real)
}
publicfunc+(lhs:Complex, rhs:Complex)->Complex{
returnComplex(real: lhs.real + rhs.real, imag: lhs.imag + rhs.imag)
}