Skip to content

Add documentation for numeric value comparison within specified accuracy #165 #314

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 8 commits into from
Apr 24, 2024

Conversation

wangxingyu5529
Copy link
Contributor

@wangxingyu5529 wangxingyu5529 commented Mar 24, 2024

Add documentation about how to compare two numeric values within specified accuracy.

Motivation:

Issue #165. swift-testing doesn't have an analog of XCAssertEquals of specified accuracy. There is no such function in the standard library. It's unclear to users how they can compare two numeric values.

Resolves #165.

Modifications:

Added a comment in the #expect macro: should use isApproximatelyEqual in the swift-numerics.

Result:

Clearer to users what numeric value comparison is.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the appropriate location for this documentation. It should be included in the migration document.

@grynspan grynspan added documentation 📚 Improvements or additions to documentation enhancement New feature or request labels Mar 24, 2024
@@ -355,6 +355,11 @@ their equivalents in the testing library:
| `try XCTUnwrap(x)` | `try #require(x)` |
| `XCTFail("…")` | `Issue.record("…")` |

- Note: When comparing two numeric values within a specified accuracy, i.e. the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@medreisbach @chuckdude Can you advise?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the modifications suggested by @heckj look great.

Copy link
Member

@heckj heckj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestions for slightly tighter and direct phrasing

@@ -355,6 +355,11 @@ their equivalents in the testing library:
| `try XCTUnwrap(x)` | `try #require(x)` |
| `XCTFail("…")` | `Issue.record("…")` |

- Note: When comparing two numeric values within a specified accuracy, i.e. the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Note: When comparing two numeric values within a specified accuracy, i.e. the
- Note: To check that two numeric values are equivalent within a specified accuracy, as in the

@@ -355,6 +355,11 @@ their equivalents in the testing library:
| `try XCTUnwrap(x)` | `try #require(x)` |
| `XCTFail("…")` | `Issue.record("…")` |

- Note: When comparing two numeric values within a specified accuracy, i.e. the
analogue of [`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal)
, please import [the swift-numerics package](https://github.com/apple/swift-numerics)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, please import [the swift-numerics package](https://github.com/apple/swift-numerics)
, import [the swift-numerics package](https://github.com/apple/swift-numerics)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also reduce the link to just "swift-numerics" but that's not a hard rule, just my preference.

- Note: When comparing two numeric values within a specified accuracy, i.e. the
analogue of [`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal)
, please import [the swift-numerics package](https://github.com/apple/swift-numerics)
and use the ``isApproximatelyEqual()`` function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and use the ``isApproximatelyEqual()`` function.
to use the ``isApproximatelyEqual()`` function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would sound odd. Simplified: "to do x, do y to do z".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flip the whole ordering, which would make it more direct, something akin to:

"Import swift-numerics and use isApproximateEqual to compare two numeric values to within an accuracy bound you specify. Use this technique as a replacement for XCTAssertEqual(::accuracy)."

or something akin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be confusing since it would introduce instructions to import swift-numerics without a reader knowing why that was important. State the problem before the solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you’re right. maybe lead with “swift-testing doesn’t include an exact duplicate of XCTAssertEqual(::accuracy)” and then jump into the direct advice.

- Note: To check that two numeric values are equivalent within a specified
accuracy, as in the analogue of
[`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal)
, please import [swift-numerics](https://github.com/apple/swift-numerics)
Copy link
Contributor

@grynspan grynspan Apr 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The comma needs to be on the same line as the link, otherwise it will be rendered with leading whitespace.
  • Remove the word "please" as overly colloquial.

analogue of [`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal)
, please import [the swift-numerics package](https://github.com/apple/swift-numerics)
and use the ``isApproximatelyEqual()`` function.
- Note: To check that two numeric values are equivalent within a specified
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Note: To check that two numeric values are equivalent within a specified
To check that two numeric values are equivalent within a specified

The callout box is not necessary here.

accuracy, as in the analogue of
[`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal)
, please import [swift-numerics](https://github.com/apple/swift-numerics)
to use the ``isApproximatelyEqual()`` function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
to use the ``isApproximatelyEqual()`` function.
and use the ``isApproximatelyEqual()`` function.

I know @heckj suggested the opposite change, but "and" would be more grammatically correct in this position. (Sorry Joe!)

, please import [the swift-numerics package](https://github.com/apple/swift-numerics)
and use the ``isApproximatelyEqual()`` function.
- Note: To check that two numeric values are equivalent within a specified
accuracy, as in the analogue of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chuckdude @medreisbach Does Apple style prefer "analogue" or "analog"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/me places his bet on "analog" - the American english spelling

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you're right, hence why I asked! 😃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 358 to 362
To check that two numeric values are equivalent within a specified
accuracy, as in the analog of
[`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal),
import [swift-numerics](https://github.com/apple/swift-numerics)
and use the ``isApproximatelyEqual()`` function.
Copy link
Contributor

@medreisbach medreisbach Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grynspan @heckj What do you think about the following?

Suggested change
To check that two numeric values are equivalent within a specified
accuracy, as in the analog of
[`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal),
import [swift-numerics](https://github.com/apple/swift-numerics)
and use the ``isApproximatelyEqual()`` function.
The testing library doesn’t provide an equivalent of [`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal). To compare two numeric values within a specified accuracy, use ``isApproximatelyEqual()`` from [swift-numerics](https://github.com/apple/swift-numerics).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop "currently"—we're not planning to add an equivalent precisely because swift-numerics is the preferred solution. We don't anticipate a change here.

"an equivalent operation for" -> "an equivalent of"

"you can use" -> "use"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the recommended change to reflect Jonathan's feedback. I only suggested "you can use" instead of "use" because I wasn't sure how direct we wanted to be. If we feel this is the "preferred solution" then "use" works much better.

The testing library doesn’t provide an equivalent of
[`XCTAssertEqual(_:_:accuracy:_:file:line:)`](https://developer.apple.com/documentation/xctest/3551607-xctassertequal).
To compare two numeric values within a specified accuracy,
use ``isApproximatelyEqual()`` from [swift-numerics](https://github.com/apple/swift-numerics).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use ``isApproximatelyEqual()`` from [swift-numerics](https://github.com/apple/swift-numerics).
use `isApproximatelyEqual()` from [swift-numerics](https://github.com/apple/swift-numerics).

(DocC uses double backticks to refer to symbols in the same package or project.)

Copy link
Contributor

@medreisbach medreisbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for adding this and making those changes.

@grynspan grynspan merged commit 286de8c into swiftlang:main Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation 📚 Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document the recommended pattern for performing floating point equality-with-accuracy expectations, analogous to XCTAssertEqual(_:_:accuracy:)
4 participants