[Mono-list] Mono and .Net Floating Point Inconsistencies
Kornél Pál
kornelpal at gmail.com
Sun Jan 18 05:54:52 EST 2009
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
>
More information about the Mono-list
mailing list