mysql float 多少位_MySql FLOAT数据类型和超过7位数字刻度的问题

bd96500e110b49cbb3cd949968f18be7.png

We are using MySql 5.0 on Ubuntu 9.04. The full version is: 5.0.75-0ubuntu10

I created a test database. and a test table in it. I see the following output from an insert statement:

mysql> CREATE TABLE test (floaty FLOAT(8,2)) engine=InnoDb;

Query OK, 0 rows affected (0.02 sec)

mysql> insert into test value(858147.11);

Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM test;

+-----------+

| floaty |

+-----------+

| 858147.12 |

+-----------+

1 row in set (0.00 sec)

There seems to be a problem with the scale/precision set up in mySql...or did I miss anything?

UPDATE:

Found a boundary for one of the numbers we were inserting, here is the code:

mysql> CREATE TABLE test (floaty FLOAT(8,2)) engine=InnoDb;

Query OK, 0 rows affected (0.03 sec)

mysql> insert into test value(131071.01);

Query OK, 1 row affected (0.01 sec)

mysql> insert into test value(131072.01);

Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM test;

+-----------+

| floaty |

+-----------+

| 131071.01 |

| 131072.02 |

+-----------+

2 rows in set (0.00 sec)

mysql>

解决方案

Face Palm!!!!

Floats are 32 bit numbers stored as mantissa and exponents. I am not 100% sure how MySql will split the storage but taking Java as an example they would use 24 bits for a signed mantissa and 8 bits for an exponent (scientific notation). This means that the maximum value a FLOAT can have is +8388608*10^127 and the minimum is -8388608*10^127. This means only 7 significant digits, and my FLOAT definition used 8.

We are going to switch all of these 8,2 to DOUBLE from FLOAT.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值