Skip to content

Commit

Permalink
Fix for bug #3661.
Browse files Browse the repository at this point in the history
Before this fix midpoint rounding mode was lost in case digits = 0.
Now this case is checked on the managed side and Round2 is only
used for digits > 0.
  • Loading branch information
Konrad M. Kruczynski committed Mar 11, 2012
1 parent 311616e commit 99d6e93
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions mcs/class/corlib/System/Math.cs
Expand Up @@ -351,6 +351,8 @@ public static double Round (double value, int digits)
{
if (digits < 0 || digits > 15)
throw new ArgumentOutOfRangeException (Locale.GetText ("Value is too small or too big."));
if (digits == 0)
return Round (value);

return Round2(value, digits, false);
}
Expand All @@ -376,6 +378,8 @@ public static double Round (double value, int digits, MidpointRounding mode)
{
if ((mode != MidpointRounding.ToEven) && (mode != MidpointRounding.AwayFromZero))
throw new ArgumentException ("The value '" + mode + "' is not valid for this usage of the type MidpointRounding.", "mode");
if (digits == 0)
return Round (value, mode);

if (mode == MidpointRounding.ToEven)
return Round (value, digits);
Expand Down
2 changes: 2 additions & 0 deletions mcs/class/corlib/Test/System/MathTest.cs
Expand Up @@ -965,6 +965,8 @@ public void TestDoubleRound3 ()

Assert.AreEqual (-63987.83593942D, Math.Round (-63987.83593942D, 8, MidpointRounding.ToEven), "#3B");
Assert.AreEqual (-63987.83593942D, Math.Round (-63987.83593942D, 8, MidpointRounding.AwayFromZero), "#3C");

Assert.AreEqual (1, Math.Round (0.5, 0, MidpointRounding.AwayFromZero));
}
#endif

Expand Down
2 changes: 0 additions & 2 deletions mono/metadata/sysmath.c
Expand Up @@ -66,8 +66,6 @@ gdouble ves_icall_System_Math_Round2 (gdouble value, gint32 digits, gboolean awa
return HUGE_VAL;
if (value == -HUGE_VAL)
return -HUGE_VAL;
if (digits == 0)
return ves_icall_System_Math_Round(value);
p = pow(10, digits);
#if defined (HAVE_ROUND) && defined (HAVE_RINT)
if (away_from_zero)
Expand Down

0 comments on commit 99d6e93

Please sign in to comment.