-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[External] [stdlib] Enhance Handling of Infinity and NaN in `assert_a…
…lmost_equal` (#38991) [External] [stdlib] Enhance Handling of Infinity and NaN in `assert_almost_equal` This PR enhances the `assert_almost_equal` function to correctly handle cases involving infinity and NaN. According to `test_assert_almost_equal` added to `/test/testing/test_assertion.mojo`, the current implementation of `assert_almost_equal` results in errors in the following cases: ```mojo alias float_type = DType.float32 alias _inf = inf[float_type]() alias _nan = nan[float_type]() ... _should_succeed( SIMD[float_type, 2](-_inf, _inf), SIMD[float_type, 2](-_inf, _inf) ) ... _should_fail( SIMD[float_type, 2](-_inf, 0.0), SIMD[float_type, 2](_inf, 0.0), rtol=0.1, ) _should_fail( SIMD[float_type, 2](_inf, 0.0), SIMD[float_type, 2](0.0, 0.0), rtol=0.1, ) ... _should_fail( SIMD[float_type, 2](_nan, 0.0), SIMD[float_type, 2](0.0, 0.0), equal_nan=True, ) ``` This PR also: - Eliminates the use of `and` and `or` in the `_isclose` function due to the issue outlined in #2374. - Explicitly reduces boolean vectors to boolean scalar values instead of counting on implicit conversions for clarity. - Avoids arithmetic operations in `_isclose` and `assert_almost_equal` when the type is boolean, as these operations are not supported in this case. - Clarifies the behavior of `assert_almost_equal` in the docstring, highlighting differences from similar functions such as `numpy.testing.assert_allclose`. - Adds the `inf` function to `utils/_numerics` along with corresponding tests in `test/utils/test_numerics.mojo`. ORIGINAL_AUTHOR=Leandro Augusto Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com> PUBLIC_PR_LINK=#2375 Co-authored-by: Leandro Augusto Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com> Closes #2375 MODULAR_ORIG_COMMIT_REV_ID: 2e8b24461dd3279bc841877cc0167acaa104f273
- Loading branch information
Showing
4 changed files
with
224 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters