Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected behavior when casting an NSNumber to Float

Tags:

ios

swift

swift4

After upgrading to Xcode 9.3 (9E145) my App showed some unexpected behavior. It seems that the issue is with a cast of an NSNumber to a Float. I use the as type cast operator for this. See the following example.

let n = NSNumber.init(value: 1.12)
let m = NSNumber.init(value: 1.00)

let x = n as? Float
let y = m as? Float

let xd = n as? Double

let z = Float(truncating: n)

Here, the first cast fails, i.e. x == nil. The second cast succeeds and the instantiation of a Float with the init:truncating constructor also succeeds, i.e. z == 1.12. The cast of n to a Double succeeds, which, to me, makes no sense at all.

Can anyone explain this behavior to me? I.e. can anyone give me a good reason why the cast of n to a Float fails? Is this a bug? If this is intended behavior, can you please reference the location in the Swift documentation that describes this?

like image 791
Sander48k Avatar asked Dec 13 '22 17:12

Sander48k


1 Answers

This is a consequence of SE-0170 NSNumber bridging and Numeric types, implemented in Swift 4:

as? for NSNumber should mean "Can I safely express the value stored in this opaque box called a NSNumber as the value I want?".

1.12 is a floating point literal, and inferred as a Double, so NSNumber(value: 1.12) is “boxing” the 64-bit floating point value closest to 1.12. Converting that to a 32-bit Float does not preserve this value:

let n = NSNumber(value: 1.12)
let x = Float(truncating: n) // Or: let x = n.floatValue
let nn = NSNumber(value: x)
print(n == nn) // false

On the other hand, 1.0 can be represented exactly as a Float:

let m = NSNumber(value: 1.0)
let y = m.floatValue
let mm = NSNumber(value: y)
print(m == mm) // true

and that is why casting m as? Float succeeds. Both

n.floatValue
Float(truncating: n)

can be used to ”truncate” the number to the closest representable 32-bit floating point value.

like image 94
Martin R Avatar answered Dec 25 '22 08:12

Martin R