
从mannual开始:
For FLOAT, the SQL standard permits an optional specification of the precision (but not the range of the exponent) in bits following the keyword FLOAT in parentheses. MySQL also supports this optional precision specification, but the precision value is used only to determine storage size. A precision from 0 to 23 results in a 4-byte single-precision FLOAT column. A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column.
我使用此命令创建了一个表
create table test (f_a float(23));
我有4个困惑:
>手册中指数的范围是什么意思而不是?
>我可以执行插入测试值(1e38);没有问题.即使我输入38位数字,它仍然有效.但是39位数字或1e39将失败.那么这个38是什么意思?
>如何检查f_a的类型转换?当我执行desc测试时;即使我在该列中插入了38位数字,f_a的类型仍然是浮动的.
>那么,量程和地球精度之间有什么区别?
1)忽略长度说明符,那只是噪音.在MySQL中,确切有两种浮点数据类型:单精度(32位)和双精度(64位).
可选的长度说明符只是提供了另一种方式来指定DOUBLE.
alter table t add foo FLOAT(4), add bar FLOAT(40) ;
相当于
alter table t add foo FLOAT , add bar DOUBLE ;
2)1e38中的38是10的指数幂.
1.0 x 10^38
在不深入所有细节的情况下,浮点数本质上表示为
sign * mantissa * (radix ^ exponent)
例如,在base10中,我们可以通过以下方式表示值123.45:
+1 * 0.12345 * ( 10 ^ 3 )
或者,我们可以使用base2,并用2的幂表示一个值(0到1之间).
3)1e39(1.0 * 10 ^ 39)大于可以用IEEE单精度FLOAT表示的值的范围. (“可表示的最大IEEE 754浮点值是2 * 2 ^ −23 * 2 ^ 27,大约是3.402×10 ^ 38.
双精度浮点支持更大范围的值,最大为10 ^ 308.
4)范围是最小值到最大值.对于DOUBLE,范围是-1 * 10 ^ 308到10 ^ 308.
精度基本上是可以表示的位数.对于单精度FLOAT,我们获得大约7个十进制数字的精度.对于DOUBLE,我们的精度约为15位十进制数字的两倍.
–
IEEE浮点并不是MySQL独有的.几乎所有现代处理器都包含浮点算术单元,它们对浮点数进行运算.
参考文献:
https://en.wikipedia.org/wiki/Single-precision_floating-point_format
https://en.wikipedia.org/wiki/Double-precision_floating-point_format
转载注明原文:MySQL float类型范围和精度混淆 - 乐贴网