Closed
Description
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;