tags:

views:

36

answers:

2

It is most important that it be accurate, but also it should take the least disk space possible.

+3  A: 

You would need a DECIMAL(6,5) to store a number from 0 to 1 with 5 decimal places.

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.1 are as follows:

  • M is the maximum number of digits (the precision). It has a range of 1 to 65.

  • D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

Daniel Vassallo
I think "no larger" means you can have DECIMAL (5,5).
yu_sha
@yu_sha, DECIMAL(5,5) has a range of .0 - .999... (no digits left of the decimal point), so that won't cut it in this case if 1 has to be included.
Tatu Ulmanen
Good point. Unfortunately, Decimal(6,5) takes 4 bytes (integer and float parts are stored separately). Might as well use float.
yu_sha
+1  A: 

According to this, in MySQL 5.0.3

  • DECIMAL(5,5) or DECIMAL (6,6) should take 3 bytes.
  • DECIMAL(4,4) 2 bytes.

If you need to store values from 0 to 1 inclusive, you might be tempted to use DECIMAL(6,5). But that occupies 4 bytes as integer and float parts are stored separately and you need one byte for integer and three for 5 decimal digits. And if you have 4 bytes you might as well use FLOAT.

Before MySql 5 DECIMALs were stored as strings and the most efficient way was to store SMALLINT or MEDIUMINT (2 or 3 bytes) and manually divide it by 10000 or 1000000 respectively.

yu_sha
Er, divide by powers of 10 for manual fixed point, surely?
bobince
If 1 is exclusive (i.e. 0 <= your data < 1) then DECIMAL(5,5) is ok; if 1 is inclusive (i.e. 0 <= your data <= 1) then DECIMAL(6,5) is needed.
Salman A
Good point. Unfortunately, Decimal(6,5) takes 4 bytes (integer and float parts are stored separately). Might as well use float.
yu_sha