The Math.Round method is expected to round a double-precision floating-point value to the nearest integral value based on the description in https://msdn.microsoft.com/en-us/library/wyk4d9cy(v=vs.110).aspx.
So, Math.Round(0.50000000000000011102230246251565404236316680908203) should return the nearest integer, 1. Instead it returns 0 on Windows and Ubuntu.
Here is the repro. The issue is with rounding the 5th element (0.50000000000000011102230246251565404236316680908203) of the values array below:
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit)]
public struct SampleUnion
{
[FieldOffset(0)] public ulong ul;
[FieldOffset(0)] public double d;
public void Display()
{
Console.WriteLine("The stored value is: 0x{0:X}", ul);
Console.WriteLine("Default rounding {0} --> {1}", d, Math.Round(d));
Console.WriteLine("Half to even {0} --> {1}", d, Math.Round(d, 0, MidpointRounding.ToEven));
Console.WriteLine("Half away from zero {0} --> {1}", d, Math.Round(d, 0, MidpointRounding.AwayFromZero));
}
}
public class Example
{
public static void Main()
{
double[] values = { -.5, -.50000000000000011102230246251565404236316680908203, -.50000000000000022204460492503130808472633361816406,
0.5, 0.50000000000000011102230246251565404236316680908203, 0.50000000000000022204460492503130808472633361816406,
1.5, 1.50000000000000022204460492503130808472633361816406,
2.5, 2.5000000000000004440892098500626161694526672363281 };
SampleUnion su = new SampleUnion();
foreach (double value in values) {
su.d = value;
su.Display();
}
}
}
The Math.Round method is expected to round a double-precision floating-point value to the nearest integral value based on the description in https://msdn.microsoft.com/en-us/library/wyk4d9cy(v=vs.110).aspx.
So, Math.Round(0.50000000000000011102230246251565404236316680908203) should return the nearest integer, 1. Instead it returns 0 on Windows and Ubuntu.
We end up calling the COMDouble::Round method in floatdouble.cpp:174, where the rounding happens.
Here is the repro. The issue is with rounding the 5th element (0.50000000000000011102230246251565404236316680908203) of the values array below: