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

ddambro ddambro at gmail.com
Mon Jan 19 00:06:47 EST 2009


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.



More information about the Mono-list mailing list