precision double

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.
 
P

Pavel Minaev

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top