stdlib-js / stdlib-js/stdlib

[RFC]: Migrate `math/base/special` packages from relative tolerance testing to ULP difference testing (tracking issue)

Open
#11,352 3 comments 2 reactions 0 assignees View on GitHub
Accepted difficulty: 1 Good First Issue JavaScript Math Modernization priority: Normal Tests
Dominant language
JavaScript
Stars
6k
Forks
1.3k
Avg merge
1d 3h
Merged PRs (30d)
611

Description

## Instructions

1. Read the issue description below.
2. Read the issue comment which follows the description.
3. Search for a package in `math/base/special` having relative tolerance tests which needs updating according to the description below.
4. Follow any additional guidance specified in this issue.

## Description

This RFC proposes migrating from Relative Tolerance testing to ULP Difference testing for `math/base/special` functions.

In short, this RFC seeks to refactor testing from, for example

```javascript
var abs = require( '@stdlib/math/base/special/abs' );

var delta = abs( y - expected[ i ] );
var tol = EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y );
```

to

```javascript
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );

t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
```

The migration is demonstrated in commit [121f2c0](https://github.com/stdlib-js/stdlib/commit/121f2c054f54a7e698da43ff6df554fd0ecae7da)

In particular, notice three things:

1. We add the import [`@stdlib/assert/is-almost-same-value`](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-same-value). See the documentation for [`@stdlib/assert/is-almost-same-value`](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-same-value) for supported parameters.
2. We remove `var delta = abs( y - expected[ i ] );` and `var tol = EPS * abs( expected[ i ] );`
3. We replace `t.ok( ... );` with `t.strictEqual( isAlmostSameValue( v, expected[ i ], 1 ), true, 'returns expected value' );` with the returned output, expected value and the **minimum required** ulp value. Also, make sure the message is `'returns expected value'` for every test case.

By performing this refactoring, we facilitate

1. Measuring error in units of the representable gap at that magnitude, which is the natural accuracy unit for floating-point.
2. Enables a uniform accuracy budget
3. Easier to test both real and complex functions, across single-precision and double-precision, with consistent and trustworthy rules.

Note: In certain functions, we may encounter different return values in the JavaScript and C implementation for the same implementation. In that case, refer to this commit [69e1068](https://github.com/stdlib-js/stdlib/commit/69e1068fd9ba96ccf49639e124c8d410b022b248). This was discussed here in detail [FAQ](https://github.com/stdlib-js/stdlib/blob/develop/docs/contributing/FAQ.md#js-vs-c-return-values).

## Steps

Given the relatively widespread practice of using relative tolerance in testing, this RFC aims to be an open call for any contributor wanting to contribute to the project to do the following:

0. Study the changes made in commit [121f2c0](https://github.com/stdlib-js/stdlib/commit/121f2c054f54a7e698da43ff6df554fd0ecae7da), as this commit contains the sorts of changes that we are looking for.
1. Ensure you have setup your local development environment by following the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md), including `make install-node-modules` and `make init`.
2. Study the documentation for [`@stdlib/assert/is-almost-same-value`](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-same-value).
3. Find a package containing relative tolerance test cases.
4. Update the test cases for that package, and **only that package**, to migrate to use ULP difference testing. Make sure that you put the **minimum required** ULP value possible. Tests can be found in the `test/` folder of a package (if it exists).
5. Run tests locally using the command `make test TESTS_FILTER=".*/math/base/special//.*"`. For example, `make test TESTS_FILTER=".*/math/base/special/hyp2f1/.*"`.
6. Verify that the updated ULP values pass all the test cases.
7. Commit your changes.
8. Submit a PR updating the test for that package (and only that package).
9. For the PR title, use the following template "test: migrate `` to ULP-based testing" where `` is the name of the package you updated (e.g., `math/base/special/hyp2f1`).
10. In the PR body/description, use the phrase "Resolves a part of ", and not "Resolves " or "Closes ", this issue, as this is a tracking issue and your PR only addresses one package of many needing updating.

### Related Issues

None

### Questions

None.

### Other

- If you are interested in working on this RFC, for each pull request, **please only update the tests for a single package**.
- As mentioned above, **please do NOT make extraneous changes to test cases. We are not interested in changing tests wholesale. Nor are we interested in new "creative" changes. We are only interested in migrating from relative tolerance testing to ULP diff based testing** Failure to match the behavior of the existing tests and to respect this guidance will result in your PRs being automatically closed without review.
- As this is a "Good First Issue", you are **strongly encouraged** to **avoid** using AI when authoring your contribution. One of the primary intents of Good First Issues is to help introduce you to stdlib, its development environment, and the contribution process, as documented in the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md). Most new contributors are unfamiliar with stdlib and its conventions, and thus fail to appropriately use LLMs and AI when authoring contributions, most often generating AI slop and leading to wasted time. Don't be one of those people. :) Take the time to manually author your first several PRs, and, once you are intimately familiar with project conventions, you can consider leveraging AI to augment your dev tasks.

### Checklist

- [x] I have read and understood the [Code of Conduct](https://github.com/stdlib-js/stdlib/blob/develop/CODE_OF_CONDUCT.md).
- [x] Searched for existing issues and pull requests.
- [x] The issue name begins with `RFC:`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.