[Mono-list] Mono and .Net Floating Point Inconsistencies

Dallman, John john.dallman at siemens.com
Mon Jan 19 05:13:11 EST 2009


> I think I've pretty much eliminated John's FPU suggestion as I went
> through all the rounding flags and none of them matched .NET, and 
> this issue occurs in both Windows and Linux.

I agree: it looks as if you've eliminated my idea. 

-- 
John Dallman
Parasolid Porting Engineer

Siemens PLM Software
46 Regent Street, Cambridge, CB2 1DP
United Kingdom
Tel: +44-1223-371554
john.dallman at siemens.com
www.siemens.com/plm

> -----Original Message-----
> From: mono-list-bounces at lists.ximian.com [mailto:mono-list-
> bounces at lists.ximian.com] On Behalf Of ddambro
> Sent: Monday, January 19, 2009 5:07 AM
> To: mono-list at lists.ximian.com
> Subject: Re: [Mono-list] Mono and .Net Floating Point Inconsistencies
> 
> 
> I get the same results using Kornél's code.  Using Robert's code
> compiled as
> x86, run on various machines, OSes, and runtimes, I get the byte values
> as:
> 
> .NET:
> 169
> 192
> 42
> 63
> 
> mono:
> 168
> 192
> 42
> 63
> 
> So if this is a rounding issue, is there any way to get the two to
> round the
> same?  I think I've pretty much eliminated John's FPU suggestion as I
> went
> through all the rounding flags and none of them matched .NET, and this
> issue
> occurs in both Windows and Linux.
> 
> 
> 
> Kornél Pál wrote:
> >
> > Hi,
> >
> > This seems to me a float32 rounding problem because when using
> float64 I
> > get the same results.
> >
> > Code:
> >
> > using System;
> >
> > class Testing
> > {
> >      public static void Main()
> >      {
> >          float f1 = 0;
> >          f1 += -0.7779751f;
> >
> > 	Console.Write("0x");
> > 	foreach (byte b in BitConverter.GetBytes(f1))
> > 		Console.Write (b.ToString("x2"));
> > 	Console.WriteLine();
> >          Console.WriteLine(f1.ToString("R"));
> >
> >          f1 += -1f * -1.42317927f;
> >
> > 	Console.Write("0x");
> > 	foreach (byte b in BitConverter.GetBytes(f1))
> > 		Console.Write (b.ToString("x2"));
> > 	Console.WriteLine();
> >          Console.WriteLine(f1.ToString("R"));
> >
> >          f1 += -1.30905056f * 0.241778925f;
> >
> > 	Console.Write("0x");
> > 	foreach (byte b in BitConverter.GetBytes(f1))
> > 		Console.Write (b.ToString("x2"));
> > 	Console.WriteLine();
> >          Console.WriteLine(f1.ToString("R"));
> >
> >          f1 = (2.0F / (1.0F + (float)Math.Exp(-4.9F * f1))) - 1.0F;
> >
> > 	Console.Write("0x");
> > 	foreach (byte b in BitConverter.GetBytes(f1))
> > 		Console.Write (b.ToString("x2"));
> > 	Console.WriteLine();
> >          Console.WriteLine(f1.ToString("R"));
> >      }
> > }
> >
> > The last value is different indeed (I only thested on Windows):
> > MS:
> > 0xa9c02a3f
> > 0,667002261
> >
> > Mono:
> > 0xa8c02a3f
> > 0,6670022
> >
> > Hex value is different for me as well.
> >
> > Kornél
> >
> > Robert Jordan wrote:
> >> ddambro wrote:
> >>> Hi,
> >>>
> >>> I was fairly sure the code had executed.  As I mentioned, different
> >>> flags in
> >>> the C code caused errors in the program.  Just to be sure though, I
> >>> threw in
> >>> some fprintf statements to stderr into setFloats() and they printed
> >>> properly, so the code was definitely executed.
> >>>
> >>> Thankfully I have been able to come up with some code that shows at
> >>> least
> >>> one example of the inconsistencies I've been talking about.  Here
> it is:
> >>
> >> It's just a string representation mismatch, as this code reveals the
> >> same bits:
> >>
> >>          Console.WriteLine(f1.ToString("R"));
> >>
> >> 	foreach (byte b in BitConverter.GetBytes(f1))
> >> 		Console.WriteLine (b);
> >>
> >> Robert
> >>
> >> _______________________________________________
> >> Mono-list maillist  -  Mono-list at lists.ximian.com
> >> http://lists.ximian.com/mailman/listinfo/mono-list
> >>
> > _______________________________________________
> > Mono-list maillist  -  Mono-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-list
> >
> >
> 
> --
> View this message in context: http://www.nabble.com/Mono-and-.Net-
> Floating-Point-Inconsistencies-tp21428695p21537058.html
> Sent from the Mono - General mailing list archive at Nabble.com.
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list


More information about the Mono-list mailing list