The storage size and range of each floating point type are planned in the following table:
So these three are floating point types in MySQL, what are the differences between them? ?
Some of you must be asking here, what is single precision and what is double precision? Let’s take a brief look at it below! We know that a byte occupies 8 bits, right? The length of float single-precision floating point type is 4x8=32 bits, so float single-precision floating point number occupies 4 bytes in memory and is described by 32 bits of binary. Then the double double-precision floating point type is ==8x8=64 bits in length==, so a double double-precision floating point number takes up 8 bytes in memory and is described using 64 bits of binary. Through calculation, 64 bits can get more mantissas! Mantissa: == is the number of digits after the decimal point == So the accuracy here mainly depends on the number of digits in the ==mantissa== part, so according to the IEEE binary floating point arithmetic standard, we can calculate and conclude:
The difference between double and float:
Advantages and disadvantages of double and float: float single precision Advantages: float single precision is faster than double double precision on some processors and only takes up half the space of double double precision Disadvantages: But when the value is very large or very small, it will become inaccurate. Advantages of double precision: Compared with float, double precision is higher, and the mantissa can have 16 bits, while float has only 7 bits of mantissa precision. Disadvantages: double precision consumes memory and is twice as much as float single precision! Double calculation speed is much slower than float, because double mantissa is more than float mantissa, so calculation must be expensive! How to choose the usage scenarios of double and float! First of all: don't use double precision when single precision is available to save memory and speed up calculations! ==Summary of double and float:== Float can represent fewer decimal places, while double can represent more decimal places and is more precise! It's that simple, just choose according to your situation! What do the lengths m and d after double and float represent? double(m,d) and float(m,d) What do m and d here represent? Many of you are also confused! Let me explain. Like the integer int(n) above, these types also have additional parameters: a display width m and a number of digits after the decimal point d. ==Summary:== In MySQL statements, when actually defining table fields, decimal type ==1. Introduction to decimal== ==2. Maximum value==
==What does the table mean? For example: == Small case 1 mysql> drop table temp2; Query OK, 0 rows affected (0.15 sec) mysql> create table temp2(id float(10,2),id2 double(10,2),id3 decimal(10,2)); Query OK, 0 rows affected (0.18 sec) mysql> insert into temp2 values(1234567.21, 1234567.21,1234567.21),(9876543.21, -> 9876543.12, 9876543.12); Query OK, 2 rows affected (0.06 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from temp2; +------------+------------+------------+ | id | id2 | id3 | +------------+------------+------------+ | 1234567.25 | 1234567.21 | 1234567.21 | | 9876543.00 | 9876543.12 | 9876543.12 | +------------+------------+------------+ 2 rows in set (0.01 sec) mysql> desc temp2; +-------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+-------+ | id | float(10,2) | YES | | NULL | | | id2 | double(10,2) | YES | | NULL | | | id3 | decimal(10,2) | YES | | NULL | | +-------+---------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) Small case 2 mysql> drop table temp2; Query OK, 0 rows affected (0.16 sec) mysql> create table temp2(id double,id2 double); Query OK, 0 rows affected (0.09 sec) mysql> insert into temp2 values(1.235,1,235); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> insert into temp2 values(1.235,1.235); Query OK, 1 row affected (0.03 sec) mysql> mysql> select * from temp2; +-------+-------+ | id | id2 | +-------+-------+ | 1.235 | 1.235 | +-------+-------+ 1 row in set (0.00 sec) mysql> insert into temp2 values(3.3,4.4); Query OK, 1 row affected (0.09 sec) mysql> select * from temp2; +-------+-------+ | id | id2 | +-------+-------+ | 1.235 | 1.235 | | 3.3 | 4.4 | +-------+-------+ 2 rows in set (0.00 sec) mysql> select id-id2 from temp2; +---------------------+ |id-id2| +---------------------+ | 0 | |-1.1000000000000005 | +---------------------+ 2 rows in set (0.00 sec) mysql> alter table temp2 modify id decimal(10,5); Query OK, 2 rows affected (0.28 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> alter table temp2 modify id2 decimal(10,5); Query OK, 2 rows affected (0.15 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from temp2; +---------+---------+ | id | id2 | +---------+---------+ | 1.23500 | 1.23500 | | 3.30000 | 4.40000 | +---------+---------+ 2 rows in set (0.00 sec) mysql> select id-id2 from temp2; +----------+ |id-id2| +----------+ | 0.00000 | |-1.10000 | +----------+ 2 rows in set (0.00 sec) This concludes this article on the differences and summary of the three floating-point types of float, double, and decimal in MySQL. For more relevant MySQL float, double, and decimal content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: 5 JavaScript Ways to Flatten Arrays
>>: How to enter and exit the Docker container
The JavaScript hasOwnProperty() method is the pro...
Events can specify the execution of SQL code once...
Table of contents Preface 1. Create objects befor...
The emergence of jQuery has greatly improved our ...
Nowadays, many websites do not allow direct copyin...
Install the unzipped version of MySql database un...
As shown below: XML/HTML CodeCopy content to clip...
Today, when I was writing a small program, I used...
Previously, the images we used were all pulled fr...
Environment Preparation Docker environment MySQL ...
Disable right-click menu <body oncontextmenu=s...
Table of contents Docker container data volume Us...
1. MySQL installed via rpm package service mysqld...
Overview Today we will mainly share how to config...
What are slots? The slot directive is v-slot, whi...