Skip to content

Commit 174068c

Browse files
Merge pull request #4 from SomeRandomiOSDev/1.1.0_Overflow
Added arithmetic operations that ignore overflow & arithmetic functions for reporting overflow for Complex numbers
2 parents 9895bd3 + d89e954 commit 174068c

File tree

3 files changed

+581
-0
lines changed

3 files changed

+581
-0
lines changed

Sources/Complex/ComplexArithmetic.swift

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,108 @@ extension Complex where Scalar: SignedNumeric {
103103
}
104104
}
105105

106+
extension Complex where Scalar: FixedWidthInteger {
107+
108+
@_transparent
109+
public static func &+ (lhs: Complex<Scalar>, rhs: Complex<Scalar>) -> Complex<Scalar> {
110+
return Complex<Scalar>(real: lhs.real &+ rhs.real, imaginary: lhs.imaginary &+ rhs.imaginary)
111+
}
112+
113+
@_transparent
114+
public static func &+= (lhs: inout Complex<Scalar>, rhs: Complex<Scalar>) {
115+
lhs = lhs &+ rhs
116+
}
117+
118+
@_transparent
119+
public static func &- (lhs: Complex<Scalar>, rhs: Complex<Scalar>) -> Complex<Scalar> {
120+
return Complex<Scalar>(real: lhs.real &- rhs.real, imaginary: lhs.imaginary &- rhs.imaginary)
121+
}
122+
123+
@_transparent
124+
public static func &-= (lhs: inout Complex<Scalar>, rhs: Complex<Scalar>) {
125+
lhs = lhs &- rhs
126+
}
127+
128+
//
129+
130+
@_transparent
131+
public static func &+ (lhs: Complex<Scalar>, rhs: Scalar) -> Complex<Scalar> {
132+
return Complex<Scalar>(real: lhs.real &+ rhs, imaginary: lhs.imaginary)
133+
}
134+
135+
@_transparent
136+
public static func &+ (lhs: Scalar, rhs: Complex<Scalar>) -> Complex<Scalar> {
137+
return Complex<Scalar>(real: lhs &+ rhs.real, imaginary: rhs.imaginary)
138+
}
139+
140+
@_transparent
141+
public static func &+= (lhs: inout Complex<Scalar>, rhs: Scalar) {
142+
lhs = lhs &+ rhs
143+
}
144+
145+
//
146+
147+
@inlinable @inline(__always)
148+
public static func .&+ (lhs: Complex<Scalar>, rhs: Complex<Scalar>) -> Complex<Scalar> {
149+
return lhs &+ rhs
150+
}
151+
152+
@inlinable @inline(__always)
153+
public static func .&+= (lhs: inout Complex<Scalar>, rhs: Complex<Scalar>) {
154+
lhs = lhs .&+ rhs
155+
}
156+
157+
//
158+
159+
@_transparent
160+
public static func &- (lhs: Complex<Scalar>, rhs: Scalar) -> Complex<Scalar> {
161+
return Complex<Scalar>(real: lhs.real &- rhs, imaginary: lhs.imaginary)
162+
}
163+
164+
@_transparent
165+
public static func &- (lhs: Scalar, rhs: Complex<Scalar>) -> Complex<Scalar> {
166+
return Complex<Scalar>(real: lhs &- rhs.real, imaginary: .zero &- rhs.imaginary)
167+
}
168+
169+
@_transparent
170+
public static func &-= (lhs: inout Complex<Scalar>, rhs: Scalar) {
171+
lhs = lhs &- rhs
172+
}
173+
174+
//
175+
176+
@inlinable @inline(__always)
177+
public static func .&- (lhs: Complex<Scalar>, rhs: Complex<Scalar>) -> Complex<Scalar> {
178+
return lhs &- rhs
179+
}
180+
181+
@inlinable @inline(__always)
182+
public static func .&-= (lhs: inout Complex<Scalar>, rhs: Complex<Scalar>) {
183+
lhs = lhs .&- rhs
184+
}
185+
}
186+
187+
extension Complex where Scalar: FixedWidthInteger {
188+
189+
@_transparent
190+
public func addingReportingOverflow(_ rhs: Complex<Scalar>) -> (partialValue: Complex<Scalar>, overflow: Bool) {
191+
let real = self.real.addingReportingOverflow(rhs.real)
192+
let imaginary = self.imaginary.addingReportingOverflow(rhs.imaginary)
193+
let overflow = real.overflow || imaginary.overflow
194+
195+
return (partialValue: Complex<Scalar>(real: real.partialValue, imaginary: imaginary.partialValue), overflow: overflow)
196+
}
197+
198+
@_transparent
199+
public func subtractingReportingOverflow(_ rhs: Complex<Scalar>) -> (partialValue: Complex<Scalar>, overflow: Bool) {
200+
let real = self.real.subtractingReportingOverflow(rhs.real)
201+
let imaginary = self.imaginary.subtractingReportingOverflow(rhs.imaginary)
202+
let overflow = real.overflow || imaginary.overflow
203+
204+
return (partialValue: Complex<Scalar>(real: real.partialValue, imaginary: imaginary.partialValue), overflow: overflow)
205+
}
206+
}
207+
106208
// MARK: - Multiplication
107209

108210
extension Complex {
@@ -150,6 +252,70 @@ extension Complex {
150252
}
151253
}
152254

255+
extension Complex where Scalar: FixedWidthInteger {
256+
257+
@_transparent
258+
public static func &* (lhs: Complex<Scalar>, rhs: Complex<Scalar>) -> Complex<Scalar> {
259+
let real = (lhs.real &* rhs.real) &- (lhs.imaginary &* rhs.imaginary)
260+
let imaginary = (lhs.real &* rhs.imaginary) &+ (lhs.imaginary &* rhs.real)
261+
262+
return Complex<Scalar>(real: real, imaginary: imaginary)
263+
}
264+
265+
@_transparent
266+
public static func &*= (lhs: inout Complex<Scalar>, rhs: Complex<Scalar>) {
267+
lhs = lhs &* rhs
268+
}
269+
270+
//
271+
272+
@_transparent
273+
public static func &* (lhs: Complex<Scalar>, rhs: Scalar) -> Complex<Scalar> {
274+
return Complex<Scalar>(real: lhs.real &* rhs, imaginary: lhs.imaginary &* rhs)
275+
}
276+
277+
@_transparent
278+
public static func &* (lhs: Scalar, rhs: Complex<Scalar>) -> Complex<Scalar> {
279+
return Complex<Scalar>(real: lhs &* rhs.real, imaginary: lhs &* rhs.imaginary)
280+
}
281+
282+
@_transparent
283+
public static func &*= (lhs: inout Complex<Scalar>, rhs: Scalar) {
284+
lhs = lhs &* rhs
285+
}
286+
287+
//
288+
289+
@_transparent
290+
public static func .&* (lhs: Complex<Scalar>, rhs: Complex<Scalar>) -> Complex<Scalar> {
291+
return Complex<Scalar>(real: lhs.real &* rhs.real, imaginary: lhs.imaginary &* rhs.imaginary)
292+
}
293+
294+
@_transparent
295+
public static func .&*= (lhs: inout Complex<Scalar>, rhs: Complex<Scalar>) {
296+
lhs = lhs .&* rhs
297+
}
298+
}
299+
300+
extension Complex where Scalar: FixedWidthInteger {
301+
302+
@_transparent
303+
public func componentwiseMultipliedFullWidth(by rhs: Complex<Scalar>) -> (high: Complex<Scalar>, low: Complex<Scalar.Magnitude>) {
304+
let real = self.real.multipliedFullWidth(by: rhs.real)
305+
let imaginary = self.imaginary.multipliedFullWidth(by: rhs.imaginary)
306+
307+
return (high: Complex<Scalar>(real: real.high, imaginary: imaginary.high), low: Complex<Scalar.Magnitude>(real: real.low, imaginary: imaginary.low))
308+
}
309+
310+
@_transparent
311+
public func componentwiseMultipliedReportingOverflow(by rhs: Complex<Scalar>) -> (partialValue: Complex<Scalar>, overflow: Bool) {
312+
let real = self.real.multipliedReportingOverflow(by: rhs.real)
313+
let imaginary = self.imaginary.multipliedReportingOverflow(by: rhs.imaginary)
314+
315+
return (partialValue: Complex<Scalar>(real: real.partialValue, imaginary: imaginary.partialValue), overflow: real.overflow || imaginary.overflow)
316+
}
317+
}
318+
153319
// MARK: - Division (BinaryInteger)
154320

155321
extension Complex where Scalar: BinaryInteger {
@@ -249,3 +415,22 @@ extension Complex where Scalar: FloatingPoint {
249415
lhs = lhs ./ rhs
250416
}
251417
}
418+
419+
extension Complex where Scalar: FixedWidthInteger {
420+
421+
@_transparent
422+
public func componentwiseDividedReportingOverflow(by rhs: Complex<Scalar>) -> (partialValue: Complex<Scalar>, overflow: Bool) {
423+
let real = self.real.dividedReportingOverflow(by: rhs.real)
424+
let imaginary = self.imaginary.dividedReportingOverflow(by: rhs.imaginary)
425+
426+
return (partialValue: Complex<Scalar>(real: real.partialValue, imaginary: imaginary.partialValue), overflow: real.overflow || imaginary.overflow)
427+
}
428+
429+
@_transparent
430+
public func componentwiseDividingFullWidth(_ dividend: (high: Complex<Scalar>, low: Complex<Scalar.Magnitude>)) -> (quotient: Complex<Scalar>, remainder: Complex<Scalar>) {
431+
let real = self.real.dividingFullWidth((high: dividend.high.real, low: dividend.low.real))
432+
let imaginary = self.imaginary.dividingFullWidth((high: dividend.high.imaginary, low: dividend.low.imaginary))
433+
434+
return (quotient: Complex<Scalar>(real: real.quotient, imaginary: imaginary.quotient), remainder: Complex<Scalar>(real: real.remainder, imaginary: imaginary.remainder))
435+
}
436+
}

Sources/Complex/Operators.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@ import Foundation
1111
infix operator .+: AdditionPrecedence
1212
infix operator .+=: AssignmentPrecedence
1313

14+
/// Component-wise addition, ignoring overflow
15+
infix operator .&+: AdditionPrecedence
16+
infix operator .&+=: AssignmentPrecedence
17+
1418
/// Component-wise subtraction
1519
infix operator .-: AdditionPrecedence
1620
infix operator .-=: AssignmentPrecedence
1721

22+
/// Component-wise subtraction, ignoring overflow
23+
infix operator .&-: AdditionPrecedence
24+
infix operator .&-=: AssignmentPrecedence
25+
1826
/// Component-wise multiplication
1927
infix operator .*: MultiplicationPrecedence
2028
infix operator .*=: AssignmentPrecedence
2129

30+
/// Component-wise multiplication, ignoring overflow
31+
infix operator .&*: MultiplicationPrecedence
32+
infix operator .&*=: AssignmentPrecedence
33+
2234
/// Component-wise division
2335
infix operator ./: MultiplicationPrecedence
2436
infix operator ./=: AssignmentPrecedence

0 commit comments

Comments
 (0)