precision double

  • Thread starter Thread starter cronusf
  • Start date Start date
C

cronusf

Consider the following program:

class Program
{
static void Main(string[] args)
{
double z = 2.3;
double x = 2.3*100;
double y = 230.0;
short s = (short)x;

Console.WriteLine(z);
Console.WriteLine(y.ToString("G17"));
Console.WriteLine(x.ToString("G17"));
Console.WriteLine(s);
}
}

Why is the output:

2.3
230
229.99999999999997
229
Press any key to continue . . .

The computer can store 230 exactly.
 
Consider the following program:

class Program
    {
        static void Main(string[] args)
        {
            double z = 2.3;
            double x = 2.3*100;
            double y = 230.0;
            short s = (short)x;

            Console.WriteLine(z);
            Console.WriteLine(y.ToString("G17"));
            Console.WriteLine(x.ToString("G17"));
            Console.WriteLine(s);
        }
    }

Why is the output:

2.3
230
229.99999999999997
229
Press any key to continue . . .

The computer can store 230 exactly.

http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Back
Top