[Mono-list] More .NET and mono floating point inconsistencies

ddambro ddambro at gmail.com
Sat Feb 14 19:35:11 EST 2009


I previously posted about some differences I noticed between the floating
point math in .NET and mono 
http://www.nabble.com/Mono-and-.Net-Floating-Point-Inconsistencies-to21428695ef1367.html
here .  The bug report can be found 
https://bugzilla.novell.com/show_bug.cgi?id=467201 here .  The patch
provided (Thank you!) fixes several issues I was encountering, but
unfortunately it led me to discover a couple more.  The following two code
samples were tested x86 machines using Linux mono 2.2 with the patch found
in the bug report, a clean Windows mono 2.0.1, and the latest version of
.NET targeting 3.0 framework and x86.

---
1.

using System;
using System.Runtime.CompilerServices;

class Testing
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void Main()
    {
        float f1=200;

        float distance=Distance(300, 500, 387.5f, 362.5f);

        float dist = 1 - (distance / f1);

        float distSqud = dist * dist;

        Console.WriteLine(distSqud.ToString("R"));

        foreach (byte b in BitConverter.GetBytes(distSqud))
            Console.WriteLine(b);

    }

    public static float Distance(float x1, float y1, float x2, float y2)
    {
        float xDist = x1 - x2;
        float yDist = y1 - y2;
        float dist = (float)Math.Sqrt(xDist * xDist + yDist * yDist);
        return dist;
    }

On .NET this code produces:
0.0342619047
54
86
12
61

and on mono it produces:
0.03426191
55
86
12
61

---
2.  

using System;
using System.Runtime.CompilerServices;

class Testing
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void Main()
    {
        float distance = Distance(616.161255f, 391.2928f, 550.8382f,
131.006973f);
        Console.WriteLine(distance.ToString("R"));

        foreach (byte b in BitConverter.GetBytes(distance))
            Console.WriteLine(b);
    }

    public static float Distance(float x1, float y1, float x2, float y2)
    {
        float xDist = x1 - x2;
        float yDist = y1 - y2;
        float dist = (float)Math.Sqrt(xDist * xDist + yDist * yDist);

        Console.WriteLine(dist.ToString("R"));

        foreach (byte b in BitConverter.GetBytes(dist))
            Console.WriteLine(b);

        return dist;
    }
}

On .NET this code produces:
268.3576
198
45
134
67
268.3576
198
45
134
67

and on mono it produces:
268.357635
199
45
134
67
268.357635
199
45
134
67

---
These seem to be very similar to the issues I was encountering before, but
they are not fixed by the patch.  What is the best way to resolve these
inconsistencies?  

Thanks,
David
-- 
View this message in context: http://www.nabble.com/More-.NET-and-mono-floating-point-inconsistencies-tp22018718p22018718.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list