Fix #1410, Apply fabs to full relative error in UtAssert_DoubleCmpRel#1558
Open
sg20180546 wants to merge 1 commit into
Open
Fix #1410, Apply fabs to full relative error in UtAssert_DoubleCmpRel#1558sg20180546 wants to merge 1 commit into
sg20180546 wants to merge 1 commit into
Conversation
…pRel UtAssert_DoubleCmpRel computed fabs((x) - (y)) / (x), applying fabs only to the numerator. When x is negative the quotient becomes negative, so the comparison (negative <= Ratio) is always true and the assertion passes regardless of how far apart x and y are (false positive). Wrap the entire relative-error expression in fabs so the comparison uses the magnitude of the relative difference: fabs(((x) - (y)) / (x)). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #1410
Describe the contribution
UtAssert_DoubleCmpRelcomputedfabs((x) - (y)) / (x), applyingfabsonly to the numerator. Because the divisor(x)keeps its sign, the relative-error expression becomes negative wheneverx < 0, so the comparison(negative) <= Ratiois always true and the assertion passes no matter how far apartxandyare (false positive).This wraps the entire relative-error expression in
fabsso the comparison uses the magnitude of the relative difference:This matches the fix concurred with by the maintainer in #1410.
Testing performed
x = -1000,y = 1000,Ratio = 1e-10fabs(-1000 - 1000) / (-1000)=-2.0→-2.0 <= 1e-10is true (incorrectly passes)fabs((-1000 - 1000) / (-1000))=2.0→2.0 <= 1e-10is false (correctly fails)x.Expected behavior changes
UtAssert_DoubleCmpRelnow correctly fails when the relative difference exceedsRatiofor negativex, instead of always passing. No change for positivex.System the issue was observed on
Additional context
File changed:
ut_assert/inc/utassert.hPer the issue discussion, this macro is not currently exercised by any active tests, so existing tests are unaffected.
Contributor Info
GitHub: @sg20180546