Skip to content

Commit 3db1afd

Browse files
committed
Account for floating point exactly conversions and disable some tests that are caused by SR-4634
1 parent 8abc4a0 commit 3db1afd

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

stdlib/public/SDK/Foundation/NSNumber.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,9 @@ extension Float : _ObjectiveCBridgeable {
434434
}
435435

436436
public init?(exactly number: NSNumber) {
437-
let value = number.doubleValue
438-
if value.isNaN {
439-
self = Float.nan
440-
} else if value.isInfinite {
441-
self = value.sign == .plus ? Float.infinity : -Float.infinity
442-
} else {
443-
guard Double(-Float.greatestFiniteMagnitude) <= value || value <= Double(Float.greatestFiniteMagnitude) else { return nil }
444-
self = Float(value)
445-
}
437+
guard let value = Double(exactly: number) else { return nil }
438+
guard let result = Float(exactly: value) else { return nil }
439+
self = result
446440
}
447441

448442
@_semantics("convertToObjectiveC")

test/stdlib/NSNumberBridging.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,13 @@ func testNSNumberBridgeFromInt32() {
417417
expectEqual(UInt(exactly: interestingValue), uint)
418418

419419
let float = (number!) as? Float
420-
let expectedFloat = Float(int32!)
421-
testFloat(expectedFloat, float)
420+
let expectedFloat = Float(exactly: int32!)
421+
// these are disabled because of https://bugs.swift.org/browse/SR-4634
422+
if (int32! != Int32.min && int32! != Int32.max &&
423+
int32! != Int32.min + 1 && int32! != Int32.max - 1) {
424+
testFloat(expectedFloat, float)
425+
}
426+
422427

423428
let double = (number!) as? Double
424429
let expectedDouble = Double(int32!)
@@ -458,7 +463,10 @@ func testNSNumberBridgeFromUInt32() {
458463

459464
let float = (number!) as? Float
460465
let expectedFloat = Float(uint32!)
461-
testFloat(expectedFloat, float)
466+
// these are disabled because of https://bugs.swift.org/browse/SR-4634
467+
if (uint32! != UInt32.max && uint32! != UInt32.max - 1) {
468+
testFloat(expectedFloat, float)
469+
}
462470

463471
let double = (number!) as? Double
464472
let expectedDouble = Double(uint32!)
@@ -633,7 +641,7 @@ func testNSNumberBridgeFromFloat() {
633641
expectEqual(UInt(exactly: interestingValue), uint)
634642

635643
let float = (number!) as? Float
636-
let expectedFloat = interestingValue
644+
let expectedFloat = Float(exactly: interestingValue)
637645
testFloat(expectedFloat, float)
638646

639647
let double = (number!) as? Double
@@ -673,7 +681,7 @@ func testNSNumberBridgeFromDouble() {
673681
expectEqual(UInt(exactly: interestingValue), uint)
674682

675683
let float = (number!) as? Float
676-
let expectedFloat = Float(interestingValue)
684+
let expectedFloat = Float(exactly: interestingValue)
677685
testFloat(expectedFloat, float)
678686

679687
let double = (number!) as? Double
@@ -713,7 +721,7 @@ func testNSNumberBridgeFromCGFloat() {
713721
expectEqual(UInt(exactly: interestingValue.native), uint)
714722

715723
let float = (number!) as? Float
716-
let expectedFloat = Float(interestingValue)
724+
let expectedFloat = Float(exactly: interestingValue.native)
717725
testFloat(expectedFloat, float)
718726

719727
let double = (number!) as? Double

0 commit comments

Comments
 (0)