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-12312] BinaryFloatingPoint._convert rounds differently from concrete floating-point initializers #54742

Closed
xwu opened this issue Mar 3, 2020 · 9 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@xwu
Copy link
Collaborator

xwu commented Mar 3, 2020

Previous ID SR-12312
Radar None
Original Reporter @xwu
Type Bug
Status Closed
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @xwu
Priority Medium

md5: c64cbc224dbcfb5a5a07fc0365a21140

Issue Description:

Consider the following example:

let a = Float.greatestFiniteMagnitude
let b = a.ulp / 2
let c = Double(a) + Double(b)
print(Float.init(c))           // inf
print(Float._convert(from: c)) // (value: 3.4028235e+38, exact: false)

Similarly, there are other mismatched conversions:

Found mismatched conversion (after 805306368 matching conversions):
 Double:   0_01111111111_0000000000000000000000110000000000000000000000000000 1.0000001788139343
 concrete: 0_01111111_00000000000000000000010 1.0000002
 generic:  0_01111111_00000000000000000000001 1.0000001
Found mismatched conversion (after 1073741823 matching conversions):
 Double:   0_01111111111_0000000000000000000001110000000000000000000000000000 1.0000004172325134
 concrete: 0_01111111_00000000000000000000100 1.0000005
 generic:  0_01111111_00000000000000000000011 1.0000004
Found mismatched conversion (after 1073741823 matching conversions):
 Double:   0_01111111111_0000000000000000000010110000000000000000000000000000 1.0000006556510925
 concrete: 0_01111111_00000000000000000000110 1.0000007
 generic:  0_01111111_00000000000000000000101 1.0000006

(Thanks to @jepers for reporting this on Swift Forums)

@xwu
Copy link
Collaborator Author

xwu commented Mar 3, 2020

#30194

@jepers
Copy link

jepers commented Apr 20, 2020

I noticed this fix didn't make it into Xcode 11.4.1 (11E503a), Swift 5.2.2. Will it not be included until 5.3?

@jepers
Copy link

jepers commented Oct 23, 2020

Is it possible to say anything about when this fix will land in an Xcode GM (It's not in Xcode 12.1)?

@xwu
Copy link
Collaborator Author

xwu commented Oct 24, 2020

Jens, that’s surprising to me. Just to confirm, what you’re trying works correctly in the nightly releases?

Update: I verified that the fix is in the release/5.3 branch on GitHub, and using Xcode 12.1, all of the following works exactly as expected after this fix:

var bitPattern: UInt64 =
  0b01111111111_0000000000000000000000110000000000000000000000000000
var z = Double(bitPattern: bitPattern)
Float._convert(from: z).value == Float(z) // true

z = Double(Float.greatestFiniteMagnitude) +
       Double(Float.greatestFiniteMagnitude.ulp / 2)
Float._convert(from: z).value == Float(z) // true

bitPattern =
  0b01111111111_0000000000000000000001110000000000000000000000000000
z = Double(bitPattern: bitPattern)
Float._convert(from: z).value == Float(z) // true

bitPattern =
  0b01111111111_0000000000000000000010110000000000000000000000000000
z = Double(bitPattern: bitPattern)
Float._convert(from: z).value == Float(z) // true

let a = Float.greatestFiniteMagnitude
let b = a.ulp / 2
let c = Double(a) + Double(b)
Float(c) == Float._convert(from: c).value // true

Are you experiencing a different result?

@jepers
Copy link

jepers commented Oct 24, 2020

Strange, those are all false for me, using (the default toolchain of) Xcode 12.1 (12A7403), in an empty command line project, and in the Terminal with swiftc:

$ swiftc --version
Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
Target: x86_64-apple-darwin19.6.0

@xwu
Copy link
Collaborator Author

xwu commented Oct 24, 2020

Works for me, but my target is different. I guess you're using a Swift standard library that shipped with the OS that pre-dates Swift 5.3.

$ swiftc --version
Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
Target: x86_64-apple-darwin20.1.0

@jepers
Copy link

jepers commented Oct 25, 2020

Huh, this is not a great user experience ... I update to the latest Xcode (12.1) via the App Store, and I don't get the latest Swift / Standard Library?
Note that the REPL doesn't say anything about the target, so my REPL screenshot looks identical to yours except that the result is false instead of true.

I don't even know (without looking that up) why my target is
x86_64-apple-darwin19.6.0 rather than eg
x86_64-apple-darwin20.1.0
or how I set it / update it.

My Software Update says "Your Mac is up to date - macOS Catalina 10.15.7 (19H2).

So how do I make sure I'm using an up to date Standard Library?
"Swift 5.3" apparently doesn't say anything at all about that ... Not even eg
"Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)"
is enough to know that my Swift/Stdlib is the same as yours ...

@xwu
Copy link
Collaborator Author

xwu commented Oct 25, 2020

I suppose that this is just one of the implications of the Swift standard library being shipped now with the OS. This function is not inlinable, so it doesn't matter what SDK you download. Your code will automatically pick up bug fixes for non-inlinable functions when running on an OS with a newer standard library. I guess in this case that means macOS 11 or later.

@jepers
Copy link

jepers commented Oct 25, 2020

I guess so, which means that it's (almost) never enough to say eg
"In Swift x.y", to specify some specific behavior or functionality, you'll always have to say
"In Swift x.y on macOS version z"
Or is there a way to retrieve the version number of the Standard Library I'm using?
BTW, see (the details of) this related forum post.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants