开发者

SQL data type for physical science measurements - double or decimal

Someone mentioned in this decimal vs double! - Which one should I use and when? post that it's best to use double for physical science computations. Does this apply to measurements such as flash point, viscosity, weight and volume? Can 开发者_StackOverflow中文版someone explain further?


Decimal = exact. That is, I have £1.23 in my pocket and applying VAT, say, is a known and accepted rounding issue.

When you measure something, you can never be exact. If something is 123 centimeters long then strictly speaking it's somewhere between 122.5 and 123.49999 centimeters long....

You are dealing with 2 different kinds of quantities.

So, use double for this.


The question you link to is about the data types in C#, not SQL, and the answers reflect that.

Decimal is a datatype used and created for dealing with currency, making sure calculations balance out (no lost cents or fractions of, for example).

Physical scientific computations rarely deal with money values, hence, you should use double whenever you need the accuracy.


Generally you would always use doubles for recording physical measurements unless you have a good reason to do otherwise. There is potentially a miniscule loss of accuracy when using any floating point number since binary is unable to perfectly represent certain decimal numbers (0.1 being the most obvious example) but the inaccuracy in the double representation is going to be many orders of magnitude smaller than the error in the measurements you take.

Decimal is used where it's very important that numbers are represented exactly so typically only when dealing with money (yes, it would seem we care more about money than science!)


Knowing which database this is for would make things easier...

Oracle only has NUMBER, which if you omit the two optional parameters precision and scale - is a float. Using both parameters is DECIMAL, and only specifying the precision is INTEGER. Can't remember how to get REAL...

MySQL Numeric data type info: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

SQL Server Numeric data type info: http://msdn.microsoft.com/en-us/library/aa258271(SQL.80).aspx

I haven't dealt with float and real much, but I've heard they aren't great for mathematical computations. I've used DECIMAL for varying precision, not just for monetary values.

What data type to use depends on the data, and how you intend to use that data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜