[Mono-dev] Double multiplication

Cedric Vivier cedricv at neonux.com
Thu Nov 29 08:15:38 EST 2007


+1

Additionnally, if you need precise calculations without round-off
errors like these you should use System.Decimal type instead of
float/double.


On Nov 29, 2007 2:10 PM, Michał Ziemski <rook at roo.k.pl> wrote:
> Hi!
>
> Floating point operations have finite precision.
> Thus 128.2 * 100 might yield 12819.99999999..., which in turn yields
> 12819 after applying Floor.
>
> I beleve you would get an analogous result from a C program.
>
> Maybe you should try rounding rather than Floor?
>
> Beast regards,
> Michał Ziemski
>
> Vladimir Dimitrov pisze:
>
> >
> > Today was one of the strangest days that I had recently. I woke up
> > today to discover another way of thinking about numbers …J
> >
> >
> >
> > To cut the long story short let me ask a simple question. Do you know
> > the result that the following code will produce as a console output:
> >
> >
> >
> > using System;
> >
> >
> >
> > namespace MultTest
> >
> > {
> >
> >     class Program
> >
> >     {
> >
> >         static void Main (string [] args)
> >
> >         {
> >
> >             double d1 = 127.2;
> >
> >             double d2 = 128.2;
> >
> >             double hundred = 100;
> >
> >
> >
> >             Console.WriteLine ("{0} * {1} = {2}", d1, hundred,
> > Math.Floor (d1 * 100));
> >
> >             Console.WriteLine ("{0} * {1} = {2}", d2, hundred,
> > Math.Floor (d2 * 100));
> >
> >             Console.ReadKey ();
> >
> >         }
> >
> >     }
> >
> > }
> >
> >
> >
> > Well what I get from it is:
> >
> >
> >
> > 127.2 * 100 = 12720
> >
> > 128.2 * 100 = 12819
> >
> >
> >
> > Am I the only one that thinks this is wrong? The things are even worse
> > because I noticed the problem on MS.NET first, and then on Mono and
> > since Microsoft is very unlikely to care about what I have to say, I
> > decided that you guys can be a lot more helpful.
> >
> >
> >
> > Thanks
> >
> >  Vladimir Dimitrov
> >
> >
> >
> > P.S. I tried it on different machines Core 2 Duo (T7200) and Pentium 4.
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > 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
>


More information about the Mono-devel-list mailing list