@@ -13,11 +13,23 @@ import XCTest
13
13
import ComplexModule
14
14
import RealModule
15
15
16
+ func ulpsFromInfinity< T: Real > ( _ a: T ) -> T {
17
+ ( . greatestFiniteMagnitude - a) / . greatestFiniteMagnitude. ulp + 1
18
+ }
19
+
16
20
// TODO: improve this to be a general-purpose complex comparison with tolerance
17
21
func relativeError< T> ( _ a: Complex < T > , _ b: Complex < T > ) -> T {
18
22
if a == b { return 0 }
19
- let scale = max ( a. magnitude, b. magnitude, T . leastNormalMagnitude) . ulp
20
- return ( a - b) . magnitude / scale
23
+ if a. isFinite && b. isFinite {
24
+ let scale = max ( a. magnitude, b. magnitude, T . leastNormalMagnitude) . ulp
25
+ return ( a - b) . magnitude / scale
26
+ } else {
27
+ if a. isFinite {
28
+ return ulpsFromInfinity ( a. magnitude)
29
+ } else {
30
+ return ulpsFromInfinity ( b. magnitude)
31
+ }
32
+ }
21
33
}
22
34
23
35
func closeEnough< T: Real > ( _ a: T , _ b: T , ulps allowed: T ) -> Bool {
@@ -32,7 +44,7 @@ func checkMultiply<T>(
32
44
let rel = relativeError ( observed, expected)
33
45
// Even if the expected result is finite, we allow overflow if
34
46
// the two-norm of the expected result overflows.
35
- if !expected . length . isFinite && !observed . isFinite { return false }
47
+ if !observed . isFinite && !expected . length . isFinite { return false }
36
48
guard rel <= allowed else {
37
49
print ( " Over-large error in \( a) * \( b) " )
38
50
print ( " Expected: \( expected) \n Observed: \( observed) " )
@@ -49,7 +61,7 @@ func checkDivide<T>(
49
61
let rel = relativeError ( observed, expected)
50
62
// Even if the expected result is finite, we allow overflow if
51
63
// the two-norm of the expected result overflows.
52
- if !expected . length . isFinite && !observed . isFinite { return false }
64
+ if !observed . isFinite && !expected . length . isFinite { return false }
53
65
guard rel <= allowed else {
54
66
print ( " Over-large error in \( a) / \( b) " )
55
67
print ( " Expected: \( expected) \n Observed: \( observed) " )
@@ -217,9 +229,7 @@ final class ArithmeticTests: XCTestCase {
217
229
218
230
}
219
231
220
- #if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
221
-
222
- /*
232
+ #if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64)) && LONG_TESTS
223
233
@available ( macOS 11 . 0 , iOS 14 . 0 , tvOS 14 . 0 , watchOS 7 . 0 , * )
224
234
func testFloat16DivisionSemiExhaustive( ) {
225
235
func complex( bitPattern: UInt32 ) -> Complex < Float16 > {
@@ -233,11 +243,10 @@ final class ArithmeticTests: XCTestCase {
233
243
if bits & 0xfffff == 0 { print ( a) }
234
244
let b = complex ( bitPattern: UInt32 . random ( in: 0 ... . max) )
235
245
var q = Complex < Float > ( a) / Complex < Float > ( b)
236
- if checkDivide(a, b, expected: Complex<Float16>(q), ulps: 32 ) { XCTFail() }
246
+ if checkDivide ( a, b, expected: Complex < Float16 > ( q) , ulps: 4 ) { XCTFail ( ) }
237
247
q = Complex < Float > ( b) / Complex < Float > ( a)
238
- if checkDivide(b, a, expected: Complex<Float16>(q), ulps: 32 ) { XCTFail() }
248
+ if checkDivide ( b, a, expected: Complex < Float16 > ( q) , ulps: 4 ) { XCTFail ( ) }
239
249
}
240
250
}
241
- */
242
251
#endif
243
252
}
0 commit comments