[Mono-dev] Double multiplication
Vladimir Dimitrov
vlad.dimitrov at gmail.com
Thu Nov 29 08:16:50 EST 2007
The problem is that in my case I cast to int and then I get this result. It
is the same as:
Console.WriteLine ("{0} * {1} = {2}", d1, hundred, (int) (d1 *
100));
plus I am not using anything very special. How can 127.2 give a right
result, and 128.2 not. This is multiplication with a whole number, after
all, so the precision should decrease not increase. Plus 128.1 will give the
right number too ...
-----Original Message-----
From: Michał Ziemski [mailto:rook at roo.k.pl]
Sent: Thursday, November 29, 2007 3:10 PM
To: Vladimir Dimitrov
Cc: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-dev] Double multiplication
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
>
More information about the Mono-devel-list
mailing list