Skip to content

Example IsApproximatelyEqual has a bug when using negative numbers #9173

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

Closed
robin-moss opened this issue Aug 1, 2023 · 0 comments · Fixed by dotnet/docs#43345
Closed

Example IsApproximatelyEqual has a bug when using negative numbers #9173

robin-moss opened this issue Aug 1, 2023 · 0 comments · Fixed by dotnet/docs#43345
Labels
area-System.Runtime help wanted Good for community contributors to help [up-for-grabs] Pri3 Indicates issues/PRs that are low priority
Milestone

Comments

@robin-moss
Copy link

The example code for IsApproximatelyEqual in single fails when you pass it negative numbers.

For example:

IsApproximatelyEqual(-10, -0.1)

returns True

This seems to be caused by the divisor being negative:

double divisor = Math.Max(value1.Value, value2.Value);
if (divisor.Equals(0))
{
    divisor = Math.Min(value1.Value, value2.Value);
}

return Math.Abs(value1.Value - value2.Value) / divisor <= epsilon;

Given two negative numbers the divisor is negative and so always <= epsilon. Testing locally a fix is to Math.Abs(divisor) or change the return line to:

return Math.Abs((value1.Value - value2.Value) / divisor) <= epsilon;
@issues-automation issues-automation bot added the Pri3 Indicates issues/PRs that are low priority label Aug 1, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 1, 2023
@gewarren gewarren added the help wanted Good for community contributors to help [up-for-grabs] label Oct 2, 2023
@krwq krwq added this to the Backlog milestone Nov 7, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Runtime help wanted Good for community contributors to help [up-for-grabs] Pri3 Indicates issues/PRs that are low priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants