Permalink
Browse files

[Foundation] Adjust Double and Float bridges to be more lenient

  • Loading branch information...
phausler committed Jun 14, 2017
1 parent 689fc28 commit c358afe6555e5e32633e879f96a3664dc7a5f3dc
@@ -451,8 +451,24 @@ extension Float : _ObjectiveCBridgeable {
}
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Float?) -> Bool {
guard let value = Float(exactly: x) else { return false }
result = value
guard let value = Double(exactly: x) else { return false }
guard !value.isNaN else {

This comment has been minimized.

Show comment
Hide comment
@stephentyrone

stephentyrone Jun 19, 2017

Member

It seems like this and the following guard should just be

guard value.isFinite else {
  result = Float(value)
  return true
}

?

@stephentyrone

stephentyrone Jun 19, 2017

Member

It seems like this and the following guard should just be

guard value.isFinite else {
  result = Float(value)
  return true
}

?

result = Float.nan
return true
}
guard !value.isInfinite else {
if value.sign == .minus {
result = -Float.infinity
} else {
result = Float.infinity
}
return true
}
guard Swift.abs(value) <= Double(Float.greatestFiniteMagnitude) else {

This comment has been minimized.

Show comment
Hide comment
@stephentyrone

stephentyrone Jun 19, 2017

Member

This condition is probably not quite right; there are Doubles greater than Float.greatestFiniteMagnitude which round down to a finite value. This should probably instead say something like:

let asFloat = Float(value)
guard asFloat.isFinite else {
  return false
}
result = asFloat
return true
@stephentyrone

stephentyrone Jun 19, 2017

Member

This condition is probably not quite right; there are Doubles greater than Float.greatestFiniteMagnitude which round down to a finite value. This should probably instead say something like:

let asFloat = Float(value)
guard asFloat.isFinite else {
  return false
}
result = asFloat
return true
return false
}
result = Float(value)
return true
}
@@ -18,6 +18,58 @@ import StdlibUnittest
import Foundation
import CoreGraphics
extension Float {
init?(reasonably value: Float) {
self = value
}
init?(reasonably value: Double) {
guard !value.isNaN else {
self = Float.nan
return
}
guard !value.isInfinite else {
if value.sign == .minus {
self = -Float.infinity
} else {
self = Float.infinity
}
return
}
guard abs(value) <= Double(Float.greatestFiniteMagnitude) else {
return nil
}
self = Float(value)
}
}
extension Double {
init?(reasonably value: Float) {
guard !value.isNaN else {
self = Double.nan
return
}
guard !value.isInfinite else {
if value.sign == .minus {
self = -Double.infinity
} else {
self = Double.infinity
}
return
}
self = Double(value)
}
init?(reasonably value: Double) {
self = value
}
}
var nsNumberBridging = TestSuite("NSNumberBridging")
func testFloat(_ lhs: Float?, _ rhs: Float?, file: String = #file, line: UInt = #line) {
@@ -645,7 +697,7 @@ func testNSNumberBridgeFromFloat() {
expectEqual(UInt(exactly: interestingValue), uint)
let float = (number!) as? Float
let expectedFloat = Float(exactly: interestingValue)
let expectedFloat = Float(reasonably: interestingValue)
testFloat(expectedFloat, float)
let double = (number!) as? Double
@@ -685,7 +737,7 @@ func testNSNumberBridgeFromDouble() {
expectEqual(UInt(exactly: interestingValue), uint)
let float = (number!) as? Float
let expectedFloat = Float(exactly: interestingValue)
let expectedFloat = Float(reasonably: interestingValue)
testFloat(expectedFloat, float)
let double = (number!) as? Double
@@ -725,7 +777,7 @@ func testNSNumberBridgeFromCGFloat() {
expectEqual(UInt(exactly: interestingValue.native), uint)
let float = (number!) as? Float
let expectedFloat = Float(exactly: interestingValue.native)
let expectedFloat = Float(reasonably: interestingValue.native)
testFloat(expectedFloat, float)
let double = (number!) as? Double
@@ -18,6 +18,58 @@ import StdlibUnittest
import Foundation
import CoreGraphics
extension Float {
init?(reasonably value: Float) {
self = value
}
init?(reasonably value: Double) {
guard !value.isNaN else {
self = Float.nan
return
}
guard !value.isInfinite else {
if value.sign == .minus {
self = -Float.infinity
} else {
self = Float.infinity
}
return
}
guard abs(value) <= Double(Float.greatestFiniteMagnitude) else {
return nil
}
self = Float(value)
}
}
extension Double {
init?(reasonably value: Float) {
guard !value.isNaN else {
self = Double.nan
return
}
guard !value.isInfinite else {
if value.sign == .minus {
self = -Double.infinity
} else {
self = Double.infinity
}
return
}
self = Double(value)
}
init?(reasonably value: Double) {
self = value
}
}
var nsNumberBridging = TestSuite("NSNumberBridgingValidation")
func testFloat(_ lhs: Float?, _ rhs: Float?, file: String = #file, line: UInt = #line) {
@@ -645,7 +697,7 @@ func testNSNumberBridgeFromFloat() {
expectEqual(UInt(exactly: interestingValue), uint)
let float = (number!) as? Float
let expectedFloat = Float(exactly: interestingValue)
let expectedFloat = Float(reasonably: interestingValue)
testFloat(expectedFloat, float)
let double = (number!) as? Double
@@ -685,7 +737,7 @@ func testNSNumberBridgeFromDouble() {
expectEqual(UInt(exactly: interestingValue), uint)
let float = (number!) as? Float
let expectedFloat = Float(exactly: interestingValue)
let expectedFloat = Float(reasonably: interestingValue)
testFloat(expectedFloat, float)
let double = (number!) as? Double
@@ -725,7 +777,7 @@ func testNSNumberBridgeFromCGFloat() {
expectEqual(UInt(exactly: interestingValue.native), uint)
let float = (number!) as? Float
let expectedFloat = Float(exactly: interestingValue.native)
let expectedFloat = Float(reasonably: interestingValue.native)
testFloat(expectedFloat, float)
let double = (number!) as? Double

0 comments on commit c358afe

Please sign in to comment.