Skip to content

[stdlib] Adjust floating-point nextUp test for NaN #15051

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

Merged
merged 1 commit into from
Mar 8, 2018
Merged

[stdlib] Adjust floating-point nextUp test for NaN #15051

merged 1 commit into from
Mar 8, 2018

Conversation

xwu
Copy link
Collaborator

@xwu xwu commented Mar 7, 2018

In #15021, we used self + 0 to silence signaling NaN, as is appropriate.

In Swift, addition of zero does not preserve the sign of NaN (although it does preserve any payload). Rather, (-Double.nan + 0).sign == .plus. Since IEEE 754-2008 §6.2 permits implementations to make their own best effort at propagating NaN diagnostics, this behavior seems to be fine.

What it does mean, however, is testing for a specific bit pattern will fail when it comes to (-Double.nan).nextUp after we change our implementation. Therefore, let's change the test before we try to restore #15021.

@xwu
Copy link
Collaborator Author

xwu commented Mar 7, 2018

@swift-ci Please test

@xwu
Copy link
Collaborator Author

xwu commented Mar 7, 2018

/cc @stephentyrone

@stephentyrone
Copy link
Contributor

stephentyrone commented Mar 7, 2018

self + 0 is actually formally the most correct result. IEEE 754 says that operations (except for the signbit operations) do not interpret the sign of NaN, so there's no expectation of what the signbit or payload should be per IEEE 754.

However, we should match the platform's behavior for NaN-propagation in arithmetic, which is precisely what self + 0 does. So there are two approaches here that would be defensible:

  • For any NaN x, require that x.nextUp and x.nextDown are some quiet NaN.
  • For any NaN x, require that x.nextUp and x.nextDown are bitwise identical to x + 0. (*)

If we choose the second option, the current implementation will fail, of course. So we should probably choose the first, and then tighten it later if we like.

(*) This is actually too strong of a condition, because IEEE 754 does not require that the same operation on the same inputs produce the same NaN encoding (a platform might, e.g. choose to encode a pointer to a tree representing a backtrace of NaN propagation through the program). It will be satisfied by every platform that Swift currently supports, but is not future proof. So we should probably choose the first option.

@xwu
Copy link
Collaborator Author

xwu commented Mar 7, 2018

Agree. Absent objections, I'll adjust the test as shown and re-apply the previous PR.

@xwu xwu merged commit 458ab92 into swiftlang:master Mar 8, 2018
@xwu xwu deleted the adjust-fp-nextup-test branch March 8, 2018 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants