Saturday, June 22, 2013

Right, choosing a data type for complex financial or mathematical calculations, in which accuracy is whopping important, should not be in a willy nilly fashion. These are some basics you need to know before you think of picking a data type in your code written in .NET framework.

1)      Floating point types have rounding problems. “float” accuracy to number of digits is almost 7, whereas number of significant digits for “double” is hovering around 15.

2)      Calculation speeds for integer arithmetic is much faster (could be double or more) than “float” and “float” faster than “double” and “double” faster than “decimal”. I mean, much much faster!

3)      So, when should you pick “decimal”. When you want 3.14 be 3.14 and not 3.14000001. “decimal” type doesn’t have rounding problem like floating point types. But it could cost you. Let’s say, If you have a billion adds/subtracts, it could slow down your program for more than 100 times as compared to “double”.

4)      Most computational functions like exponents, sines are not available for the “decimal” type or at least you have to do an explicit conversion at some point which means possible “rounding off”.