Checking in the new database structure I saw that someone changed a field from float to double. Wondering why, I checked the mysql documentation, but honestly didn't understand what the difference is.
在检查新的数据库结构时,我看到有人将字段从float更改为double。我想知道为什么,我检查了mysql文档,但是老实说我不明白其中的区别。
Can someone explain?
谁能解释一下吗?
6 个解决方案
#1
79
They both represent floating point numbers. A FLOAT
is for single-precision, while a DOUBLE
is for double-precision numbers.
它们都表示浮点数。浮点数用于单精度,双精度数用于双精度数。
MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
MySQL对单精度值使用4字节,双精度值使用8字节。
There is a big difference from floating point numbers and decimal (numeric) numbers, which you can use with the DECIMAL
data type. This is used to store exact numeric data values, unlike floating point numbers, where it is important to preserve exact precision, for example with monetary data.
与浮点数和十进制(数字)数有很大的不同,您可以使用十进制数据类型。它用于存储精确的数值数据值,不像浮点数,在浮点数中,保持精确的精度很重要,例如货币数据。
#2
54
Perhaps this example could explain.
也许这个例子可以解释。
CREATE TABLE `test`(`fla` FLOAT,`flb` FLOAT,`dba` DOUBLE(10,2),`dbb` DOUBLE(10,2));
We have a table like this :
我们有这样一张桌子:
+-------+-------------+
| Field | Type |
+-------+-------------+
| fla | float |
| flb | float |
| dba | double(10,2)|
| dbb | double(10,2)|
+-------+-------------+
For first difference, we try to insert a record with '1.2' to each field :
首先,我们尝试在每个字段中插入一个“1.2”的记录:
INSERT INTO `test` values (1.2,1.2,1.2,1.2);
The table showing like this :
如表所示:
SELECT * FROM `test`;
+------+------+------+------+
| fla | flb | dba | dbb |
+------+------+------+------+
| 1.2 | 1.2 | 1.20 | 1.20 |
+------+------+------+------+
See the different?
看到不同的吗?
We try to next example:
我们尝试下一个例子:
SELECT fla+flb, dba+dbb FROM `test`;
Hola! We can find the different like this :
你好!我们可以找到这样的不同:
+--------------------+---------+
| fla+flb | dba+dbb |
+--------------------+---------+
| 2.4000000953674316 | 2.40 |
+--------------------+---------+
#3
26
Doubles are just like floats, except for the fact that they are twice as large. This allows for a greater accuracy.
双打就像浮点数,除了它们的大小是浮点数的两倍。这允许更高的准确度。
#4
10
Float has 32 bit (4 bytes) with 8 places accuracy. Double has 64 bit (8 bytes) with 16 places accuracy.
浮点有32位(4字节),8位精度。Double有64位(8字节),精度16位。
If you need better accuracy, use Double instead of Float.
如果你需要更好的准确性,使用双精度而不是浮动。
#5
8
FLOAT stores floating point numbers with accuracy up to eight places and has four bytes while DOUBLE stores floating point numbers with accuracy upto 18 places and has eight bytes.
浮点存储浮点数,精度最高可达8个位置,并且有4个字节,双存储浮点数,精度高达18位,有8个字节。
#6
3
Thought I'd add my own example that helped me see the difference using the value 1.3
when adding or multiplying with another float
, decimal
, and double
.
我想我应该添加我自己的示例,它帮助我看到在使用值1.3与另一个浮点数、小数和双精度数相加或相乘时的区别。
1.3
float ADDED to 1.3
of different types:
1.3添加到1.3不同类型的浮动:
|float | double | decimal |
+-------------------+------------+-----+
|2.5999999046325684 | 2.6 | 2.60000 |
1.3
float MULTIPLIED by 1.3
of different types:
1.3浮动乘以1.3的不同类型:
| float | double | decimal |
+--------------------+--------------------+--------------+
| 1.6899998760223411 | 1.6900000000000002 | 1.6900000000 |
This is using MySQL 6.7
这是用的是0。7。
Query:
查询:
SELECT
float_1 + float_2 as 'float add',
double_1 + double_2 as 'double add',
decimal_1 + decimal_2 as 'decimal add',
float_1 * float_2 as 'float multiply',
double_1 * double_2 as 'double multiply',
decimal_1 * decimal_2 as 'decimal multiply'
FROM numerics
Create Table and Insert Data:
创建表格并插入数据:
CREATE TABLE `numerics` (
`float_1` float DEFAULT NULL,
`float_2` float DEFAULT NULL,
`double_1` double DEFAULT NULL,
`double_2` double DEFAULT NULL,
`decimal_1` decimal(10,5) DEFAULT NULL,
`decimal_2` decimal(10,5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `_numerics`
(
`float_1`,
`float_2`,
`double_1`,
`double_2`,
`decimal_1`,
`decimal_2`
)
VALUES
(
1.3,
1.3,
1.3,
1.3,
1.30000,
1.30000
);
#1
79
They both represent floating point numbers. A FLOAT
is for single-precision, while a DOUBLE
is for double-precision numbers.
它们都表示浮点数。浮点数用于单精度,双精度数用于双精度数。
MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
MySQL对单精度值使用4字节,双精度值使用8字节。
There is a big difference from floating point numbers and decimal (numeric) numbers, which you can use with the DECIMAL
data type. This is used to store exact numeric data values, unlike floating point numbers, where it is important to preserve exact precision, for example with monetary data.
与浮点数和十进制(数字)数有很大的不同,您可以使用十进制数据类型。它用于存储精确的数值数据值,不像浮点数,在浮点数中,保持精确的精度很重要,例如货币数据。
#2
54
Perhaps this example could explain.
也许这个例子可以解释。
CREATE TABLE `test`(`fla` FLOAT,`flb` FLOAT,`dba` DOUBLE(10,2),`dbb` DOUBLE(10,2));
We have a table like this :
我们有这样一张桌子:
+-------+-------------+
| Field | Type |
+-------+-------------+
| fla | float |
| flb | float |
| dba | double(10,2)|
| dbb | double(10,2)|
+-------+-------------+
For first difference, we try to insert a record with '1.2' to each field :
首先,我们尝试在每个字段中插入一个“1.2”的记录:
INSERT INTO `test` values (1.2,1.2,1.2,1.2);
The table showing like this :
如表所示:
SELECT * FROM `test`;
+------+------+------+------+
| fla | flb | dba | dbb |
+------+------+------+------+
| 1.2 | 1.2 | 1.20 | 1.20 |
+------+------+------+------+
See the different?
看到不同的吗?
We try to next example:
我们尝试下一个例子:
SELECT fla+flb, dba+dbb FROM `test`;
Hola! We can find the different like this :
你好!我们可以找到这样的不同:
+--------------------+---------+
| fla+flb | dba+dbb |
+--------------------+---------+
| 2.4000000953674316 | 2.40 |
+--------------------+---------+
#3
26
Doubles are just like floats, except for the fact that they are twice as large. This allows for a greater accuracy.
双打就像浮点数,除了它们的大小是浮点数的两倍。这允许更高的准确度。
#4
10
Float has 32 bit (4 bytes) with 8 places accuracy. Double has 64 bit (8 bytes) with 16 places accuracy.
浮点有32位(4字节),8位精度。Double有64位(8字节),精度16位。
If you need better accuracy, use Double instead of Float.
如果你需要更好的准确性,使用双精度而不是浮动。
#5
8
FLOAT stores floating point numbers with accuracy up to eight places and has four bytes while DOUBLE stores floating point numbers with accuracy upto 18 places and has eight bytes.
浮点存储浮点数,精度最高可达8个位置,并且有4个字节,双存储浮点数,精度高达18位,有8个字节。
#6
3
Thought I'd add my own example that helped me see the difference using the value 1.3
when adding or multiplying with another float
, decimal
, and double
.
我想我应该添加我自己的示例,它帮助我看到在使用值1.3与另一个浮点数、小数和双精度数相加或相乘时的区别。
1.3
float ADDED to 1.3
of different types:
1.3添加到1.3不同类型的浮动:
|float | double | decimal |
+-------------------+------------+-----+
|2.5999999046325684 | 2.6 | 2.60000 |
1.3
float MULTIPLIED by 1.3
of different types:
1.3浮动乘以1.3的不同类型:
| float | double | decimal |
+--------------------+--------------------+--------------+
| 1.6899998760223411 | 1.6900000000000002 | 1.6900000000 |
This is using MySQL 6.7
这是用的是0。7。
Query:
查询:
SELECT
float_1 + float_2 as 'float add',
double_1 + double_2 as 'double add',
decimal_1 + decimal_2 as 'decimal add',
float_1 * float_2 as 'float multiply',
double_1 * double_2 as 'double multiply',
decimal_1 * decimal_2 as 'decimal multiply'
FROM numerics
Create Table and Insert Data:
创建表格并插入数据:
CREATE TABLE `numerics` (
`float_1` float DEFAULT NULL,
`float_2` float DEFAULT NULL,
`double_1` double DEFAULT NULL,
`double_2` double DEFAULT NULL,
`decimal_1` decimal(10,5) DEFAULT NULL,
`decimal_2` decimal(10,5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `_numerics`
(
`float_1`,
`float_2`,
`double_1`,
`double_2`,
`decimal_1`,
`decimal_2`
)
VALUES
(
1.3,
1.3,
1.3,
1.3,
1.30000,
1.30000
);