tidb
tidb copied to clipboard
Insert data to a float field with 2 digits in TiDB, got float with N digits
It seems data stored in float fields , not equal to what inserted. See the codes below:
mysql> create table test2(f float(10,2),db double(10,2), de decimal(10,2));
Query OK, 0 rows affected (1.01 sec)
mysql> insert into test2(f,db,de) values(1.11,1.11,1.11);
Query OK, 1 row affected (0.01 sec)
mysql> insert into test2(f,db,de) values(1.111,1.111,1.111);
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select *,f-1.11,db-1.11,de-1.11 from test2;
+------+------+------+----------------------------+---------+---------+
| f | db | de | f-1.11 | db-1.11 | de-1.11 |
+------+------+------+----------------------------+---------+---------+
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 | 0 | 0 |
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 | 0 | 0 |
+------+------+------+----------------------------+---------+---------+
2 rows in set (0.00 sec)
Related post in stackoverflow: https://stackoverflow.com/questions/49402280/sum-a-float-field-with-2-digits-in-tidb-got-float-with-8-digits
@calmzealA Thank you for your feedback. We're working on this.
@XuHuaiyu stille not fixed in TiDB 2.0.2
@calmzealA Thank you for your feedback. I'll try to fix it this week.
Confirming this is still an issue:
mysql [localhost:8019] {msandbox} (test) > select *,f-1.11,db-1.11,de-1.11 from test2;
+------+------+------+--------+---------+---------+
| f | db | de | f-1.11 | db-1.11 | de-1.11 |
+------+------+------+--------+---------+---------+
| 1.11 | 1.11 | 1.11 | 0.00 | 0.00 | 0.00 |
| 1.11 | 1.11 | 1.11 | 0.00 | 0.00 | 0.00 |
+------+------+------+--------+---------+---------+
2 rows in set (0.00 sec)
tidb> select *,f-1.11,db-1.11,de-1.11 from test2;
+------+------+------+----------------------------+---------+---------+
| f | db | de | f-1.11 | db-1.11 | de-1.11 |
+------+------+------+----------------------------+---------+---------+
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 | 0 | 0.00 |
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 | 0 | 0.00 |
+------+------+------+----------------------------+---------+---------+
2 rows in set (0.00 sec)
tidb> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.0-beta.2-183-g57db6cec7
Git Commit Hash: 57db6cec7ffad78e74b6ac6c67a2bfe9a6718d17
Git Branch: master
UTC Build Time: 2020-04-04 08:06:18
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.00 sec)
Including a pasteable testcase:
drop table if exists test2;
create table test2(f float(10,2),db double(10,2), de decimal(10,2));
insert into test2(f,db,de) values(1.11,1.11,1.11);
insert into test2(f,db,de) values(1.111,1.111,1.111);
select *,f-1.11,db-1.11,de-1.11 from test2;
..
mysql> select *,f-1.11,db-1.11,de-1.11 from test2;
+------+------+------+----------------------------+---------+---------+
| f | db | de | f-1.11 | db-1.11 | de-1.11 |
+------+------+------+----------------------------+---------+---------+
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 | 0 | 0.00 |
| 1.11 | 1.11 | 1.11 | 0.000000014305114648394124 | 0 | 0.00 |
+------+------+------+----------------------------+---------+---------+
2 rows in set (0.00 sec)
mysql> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.0-beta.2-893-g4e829aaee
Edition: Community
Git Commit Hash: 4e829aaee7b656aa807814708ae05af5233302af
Git Branch: master
UTC Build Time: 2020-08-04 12:40:52
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.00 sec)
Vs MySQL:
mysql [localhost:5731] {msandbox} (test) > select *,f-1.11,db-1.11,de-1.11 from test2;
+------+------+------+--------+---------+---------+
| f | db | de | f-1.11 | db-1.11 | de-1.11 |
+------+------+------+--------+---------+---------+
| 1.11 | 1.11 | 1.11 | 0.00 | 0.00 | 0.00 |
| 1.11 | 1.11 | 1.11 | 0.00 | 0.00 | 0.00 |
+------+------+------+--------+---------+---------+
2 rows in set (0.00 sec)