[Mono-dev] Simple floating maths problem
Rolf Bjarne Kvinge
rolflists at ya.com
Fri Apr 30 09:21:30 EDT 2010
Hi,
Running this modified simple on x86 .NET:
int fact = 50000;
float [] tList = { 0.95864f, 0.89374f, 0.89092f,
0.89716f, 0.4191f, 0.79782f };
foreach (float val in tList) {
int comp = (int) (val * fact);
float compf = (val * fact);
int compi = (int) compf;
Console.WriteLine ("{0} * {1} = {2} = {3} =
{4}", val, fact, comp, compf, compi);
}
Gives this result:
0,95864 * 50000 = 47931 = 47932 = 47932
0,89374 * 50000 = 44686 = 44687 = 44687
0,89092 * 50000 = 44545 = 44546 = 44546
0,89716 * 50000 = 44857 = 44858 = 44858
0,4191 * 50000 = 20954 = 20955 = 20955
0,79782 * 50000 = 39890 = 39891 = 39891
I think it's a precision issue you're running into, as Robert said.
Rolf
> -----Mensaje original-----
> De: mono-devel-list-bounces at lists.ximian.com [mailto:mono-devel-list-
> bounces at lists.ximian.com] En nombre de Stuart Fraser
> Enviado el: viernes, 30 de abril de 2010 0:22
> Para: mono-devel-list at lists.ximian.com
> Asunto: [Mono-dev] Simple floating maths problem
>
> HI All,
>
> I'm currently working on some code for HPC benchmarking using a number
> of languages and runtimes but am primarily focussing on C#. During my
> coding I have come across some inconsistencies between results between
> calculated in Mono vs .Net (and C /Java), there may be a number of
> reasons for this but I was after a sanity check before I start looking
> into the mono runtime code...
>
> Basically I have pulled out some of the internal values/working and
> problem is thus:
>
> int fact = 50000;
> float[] tList = { 0.95864f, 0.89374f, 0.89092f, 0.89716f,
> 0.4191f, 0.79782f };
>
> foreach (float val in tList)
> {
> int comp = (int)(val * fact);
> Console.WriteLine("{0:g} * {1:d} = {2:d}", val, fact,
> comp);
> }
>
> Mono results:
> 0.95864 * 50000 = 47931
> 0.89374 * 50000 = 44686
> 0.89092 * 50000 = 44545
> 0.89716 * 50000 = 44857
> 0.4191 * 50000 = 20954
> 0.79782 * 50000 = 39890
>
> .Net results :
> 0.95864 * 50000 = 47932
> 0.89374 * 50000 = 44687
> 0.89092 * 50000 = 44546
> 0.89716 * 50000 = 44858
> 0.4191 * 50000 = 20955
> 0.79782 * 50000 = 39891
>
> Obviously precision required is very important but this only appears to
> be a problem on Mono.
> I have tested this with Mono 2.6.2 and 2.6.4 on
> - OpenSUSE 11.2 (x64) AMD Athlon x64
> - Windows 7 (x64) Intel i7
> - Cray XT4h (x64) AMD Opteron :)
> I have tried the code being run checked and unchecked, I have compiled
> with .net and run the executable under mono and done a clean mono build
> and test,
> I have looked through the bugzilla but haven't seen anything that
> matches this, its late and I'm tired is this a bug in the mono
> jit/runtime?
> Suggestions on where to look for a fix?
>
> Regards
> Stuart
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
More information about the Mono-devel-list
mailing list