MySQL:float 和 double 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2160810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: What's the difference between float and double?
提问by janpio
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?
有人可以解释一下吗?
采纳答案by Daniel Vassallo
They both represent floating point numbers. A FLOAT
is for single-precision, while a DOUBLE
is for double-precision numbers.
它们都代表浮点数。AFLOAT
表示单精度,而 aDOUBLE
表示双精度数。
MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
MySQL 对单精度值使用四个字节,对双精度值使用八个字节。
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.
浮点数和十进制(数字)数有很大的不同,您可以将它们与DECIMAL
数据类型一起使用。这用于存储精确的数字数据值,与浮点数不同,在浮点数中保持精确的精度很重要,例如货币数据。
回答by Andi S.
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 |
+--------------------+---------+
回答by Williham Totland
Doubles are just like floats, except for the fact that they are twice as large. This allows for a greater accuracy.
双打就像浮动一样,除了它们是两倍大。这允许更高的准确性。
回答by Omar
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
添加或与另一个相乘时float
,decimal
和double
。
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
float MULTIPLIED by1.3
不同类型:
| float | double | decimal |
+--------------------+--------------------+--------------+
| 1.6899998760223411 | 1.6900000000000002 | 1.6900000000 |
This is using MySQL 6.7
这是使用 MySQL 6.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
);
回答by Ravi Patel
Floathas 32 bit (4 bytes) with 8 places accuracy. Doublehas 64 bit (8 bytes) with 16 places accuracy.
Float有 32 位(4 字节),8 位精度。 双精度为 64 位(8 字节),精度为 16 位。
If you need better accuracy, use Doubleinstead of Float.
如果您需要更高的准确性,请使用Double而不是Float。
回答by user3902486
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.
FLOAT 存储精度高达 8 位的浮点数,有 4 个字节,而 DOUBLE 存储精度高达 18 位的浮点数,有 8 个字节。