Tuesday, April 3, 2012

Float, Double and Decimal

There are many ways to store number value. Let’s pick three variable types of numbers that we always use: float, double and decimal. What are those differences?


decimal dd = 1234567890.123456789712345678971234567897M;

            double db = Convert.ToDouble(dd) * 1;
            float fl = (float)dd * 1;
            decimal dc = dd * 1;
            txtdecimal.Text = dc.ToString("f20");
            txtdouble.Text = db.ToString("f20");
            txtfloat.Text = fl.ToString("f20");


result:


1. float able to store 7 digit precision
2. double can store 16 digit precision
3. decimal can store up to 27 digit precision -> this is a surprise. even decimal can be out of range with less then 30 digit precision!

Currently I am researching on OpenCL and OpenCL allows ONLY double and float variable. Therefore graphic card is not suitable for task that requires high precision. OpenCL is more suitable for simulation or mass calculation like CAD CAM tools. Will update more information on OpenCL + c# when research is done.



No comments: