# [Mono-dev] Simple floating maths problem

Stuart Fraser stuart at stuartfraser.net
Fri Apr 30 16:39:03 EDT 2010

```Indeed, agreed on the floating point results but it would be nice to have some consistency and preferably accuracy.
Currently I'm writing my dissertation for an MSc in HPC and trying to justify the use of Mono & C# for use in Scientific and HPC codes, the performance is good across the three basic tests I'm using (Dense Linear Algebra, Sparse Linear Algebra and Spectral Methods [Based on the Java Grande Test suite written by my lecturers at EPCC]).
Performance is certainly comparable with .Net and  Java for the majority of cases and you don't run .Net on the Cray. I know however 'correctness' would be considered a major issue by my academic supervisors :)  I'm hoping though that this situation is somewhat synthetic but after exams I will look into this more deeply.

Thanks
Stuart

> -----Original Message-----
> From: Andreas Nahr [mailto:ClassDevelopment at A-SoftTech.com]
> Sent: 30 April 2010 21:16
> To: 'Rolf Bjarne Kvinge'; Stuart Fraser; mono-devel-list at lists.ximian.com
> Subject: AW: [Mono-dev] Simple floating maths problem
>
> Sorry, somehow I got this wrong.
>
> So in fact I also get the
> 47931 = 47932 = 47932 for x86 .Net 3.5 (and Mono) and
> 47932 = 47932 = 47932 for x64 .Net 3.5
>
> Well there is a reason why you should never rely on floating point results for
> producing exact (integer) values.
>
> Andreas
>
> -----Ursprüngliche Nachricht-----
> Von: mono-devel-list-bounces at lists.ximian.com
> [mailto:mono-devel-list-bounces at lists.ximian.com] Im Auftrag von Andreas
> Nahr
> Gesendet: Freitag, 30. April 2010 22:06
> An: 'Rolf Bjarne Kvinge'; 'Stuart Fraser'; mono-devel-list at lists.ximian.com
> Betreff: Re: [Mono-dev] Simple floating maths problem
>
> I've been running your modified sample under x86 .Net 3.5 and my result is:
> 0,95864 * 50000 = 47932 = 47932 = 47932
> 0,89374 * 50000 = 44687 = 44687 = 44687
> 0,89092 * 50000 = 44546 = 44546 = 44546
> 0,89716 * 50000 = 44858 = 44858 = 44858
> 0,4191 * 50000 = 20955 = 20955 = 20955
> 0,79782 * 50000 = 39891 = 39891 = 39891
> and running it under x64 .Net 3.5 is exactly the same as above.
>
> Only running mono on the same machine results in:
> 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
>
> So something is fishy *g*.
> Maybe I'm wrong, but if it was a precision error, then how could compi be
> different from comp?
>
> Andreas
>
> -----Ursprüngliche Nachricht-----
> Von: mono-devel-list-bounces at lists.ximian.com
> [mailto:mono-devel-list-bounces at lists.ximian.com] Im Auftrag von Rolf
> Bjarne Kvinge
> Gesendet: Freitag, 30. April 2010 15:22
> An: 'Stuart Fraser'; mono-devel-list at lists.ximian.com
> Betreff: Re: [Mono-dev] Simple floating maths problem
>
> 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);
> >             }
> >
> > 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.
> >
> __________________________________________________________
> ____________
> > _______________________________________________
> > Mono-devel-list mailing list
> > Mono-devel-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
> __________________________________________________________
> ____________
> This email has been scanned by the MessageLabs Email Security System.