Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-5179] NSNumber -> Float bridging in Swift 3.2 is not backwards-compatible #4086

Closed
swift-ci opened this issue Jun 9, 2017 · 3 comments
Closed

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Jun 9, 2017

Previous ID SR-5179
Radar rdar://problem/32792779
Original Reporter tgoyne (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: ad8e2cadad5a75d75681cc31a9473d72

is duplicated by:

  • SR-5228 NSNumber casting to Float returns nil

Issue Description:

$ cat test.swift

import Foundation
let value = NSNumber(value: 0.1)
let double = value as! Double
let float = value as! Float
print("\(float) \(double)")

$ DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcrun swift -version
Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42)
Target: x86_64-apple-macosx10.9

$ DEVELOPER_DIR=/Applications/Xcode-Beta.app/Contents/Developer xcrun swift -version
Apple Swift version 4.0 (swiftlang-900.0.43 clang-900.0.22.8)
Target: x86_64-apple-macosx10.9

$ DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcrun swift ../test.swift
0.1 0.1

$ DEVELOPER_DIR=/Applications/Xcode-Beta.app/Contents/Developer xcrun swift -swift-version 3 test.swift
fatal error: Unable to bridge NSNumber to Float: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.43/src/swift/stdlib/public/SDK/Foundation/NSNumber.swift, line 449

Looking at NSNumber.swift I can see that it's failing because the bridging conversion now requires that the value be exactly representable as a Float, which is a silent breaking change that applies even when in Swift 3.2 mode.

@belkadan
Copy link

Swift 3.2 is source-compatible with Swift 3.1, but may still have some behavioral changes. It does seem unfortunate that this one affects relatively simple values like 0.1, though—maybe our check for "exactly" equal isn't the best one. @phausler?

@belkadan
Copy link

@swift-ci create

@swift-ci
Copy link
Contributor Author

swift-ci commented Aug 2, 2017

Comment by Matt Rajca (JIRA)

Looks fixed in Xcode 9 seed 4:

value: NSNumber = Double(0.1)
double: Double = 0.10000000000000001
float: Float = 0.100000001

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants