关于decimal非常浅显的学习与整理

发布时间 2023-08-20 21:54:06作者: 济南小老虎

关于decimal非常浅显的学习与整理


背景知识

整数,小数,浮点,定点
整数(Integer)是没有小数部分的数值,可以是正数、负数或零。在计算机中,整数通常以二进制形式存储。

小数(Decimal)是带有小数部分的数值。小数可以是有限的,也可以是无限循环的。在计算机中,小数通常以浮点数或定点数的形式存储。

浮点数(Floating-point)是一种用于表示带有小数部分的数值的方法。浮点数使用科学计数法表示,包括一个有效数字和一个指数。在计算机中,浮点数通常使用IEEE 754标准进行存储。

定点数(Fixed-point)是一种用于表示带有小数部分的数值的方法。定点数使用固定的小数点位置来表示数值。在计算机中,定点数通常以整数形式存储,并使用一个固定的缩放因子来确定小数点位置。

关于金额类型

decimal 其实在不同的编程语言, 不同的数据库里面的实现和使用都是不一样的
但是有一点是明确的:
浮点类型, 不管是float还是double 都是非精确数值类型
都会有各种除不尽的尾差问题. 

但是decimal 这种数值类型, 是精确的数值. 

decimal是一种数据类型,用于存储精确的十进制数值。它通常用于需要精确计算和存储小数的场景,例如财务应用程序或需要保留小数位数的计算。
在大多数编程语言和数据库系统中,decimal类型可以指定精度和范围。精度表示数据的总位数,而范围表示小数的小数位数。例如,decimal(10, 2)表示精度为10,小数位数为2的decimal类型。

尾差的一个展示

参照网站: https://www.cnblogs.com/danielzzz/p/16824214.html

CREATE TABLE test_float(
`f1` FLOAT,
`f2` DOUBLE,
`f3` DEC(10,2)
);

INSERT INTO test_float(f1,f2,f3) VALUES(0.47,0.47,0.47),(0.44,0.44,0.44),(0.19,0.19,0.19);

SELECT SUM(f1),SUM(f2),SUM(f3) FROM test_float;

测试结果为:
SUM(f1)             	SUM(f2)	               SUM(f3)
1.0999999940395355	1.0999999999999999	1.10

发现只有decimal 才可以精确表示数值. float和double 其实都是有小数位数的. 

关于decimal的数据库存储

自己查询了不少资料, 发现不同数据库, 比如MySQL和SQLServer 都不一样. 
MySQL 貌似采用int 类型存储 9位精确数字. 
也就是很多资料里面提到的 四个字节存储 9个数字. 
但是SQLServer的存储模式就是 1个字节存储连个数字. 

MySQL应该会有一个单独的表示符号的布尔类型的设置
整数部分和小数部分是单独进行存储. 

SQLServer的存储模式暂时还不是很清楚. 

初步怀疑 MySQL其实是采用 int类型 能够表示 42亿 也就是部分10位数的情况 
退而求其次用来表示 9位精确数字. 

SQLServer的模式 其实就是 一个字节,可以表示 256个数字. 
退而求其次表示 99 这个最大的两位数值. 

所以每个字节表示两位数. 用来进行精确数字的存储. 

但是MySQL与SQLserver 可能还比较相似. 但是与CK的存储模式可能还不一样. 

需要说明大部分资料都确认, decimal的性能是数值类型里面最差的. 除非是金额汇率等必须精确的数字

整数可以用int. 逼不得已在用decimal 

自己查了不少资料, 本来想dump 不分 raw data 但是发现没有达到自己的预期目标. 

所以几天仅是简单的进行一些汇总. 还需要较多的时间研究底层的存储模式. 

看到了 ibd2sdi 以及hexedit等工具. 但是都没有达到自己的预期. 

因为是一个周天, 大部分时间用于陪伴自己的孩子了. 学习的时间不是很充足. 

搜易今天仅是一个简单的学习与整理. 后续还需要继续探究和提高. 

阿里上面一个说明-未看懂

# 其实原理看明白了. 但是不知道数据是怎么来的
# 感觉mysql的确做的比较简单.可能浪费了部分存储空间. 但是处理起来比较简单.
# 所有的性能其实都是在调和和折中. 自己哪方面强. 哪方面可能就要多出点力. 
# 数据库与人生不一样, 强的必须得像低的低头. 人生是长板决定你的上限. 计算机是短板决定你的上限.

Synopsis
Decimal2bin ()
From-value to convert
To-points to buffer Where string representation shoshould be stored
Precision/scale-see decimal_bin_size () below

Note
The buffer is assumed to be of the size decimal_bin_size (precision, scale)

Return Value
E_dec_ OK/e_dec_truncated/e_dec_overflow

Description
For storage decimal numbers are converted to the "binary" format.

This format has the following properties:
1. Length of the binary representation depends on the {precision, scale}
As provided by the caller and not on the intg/frac of the decimal
Convert.
2. Binary representations of the same {precision, scale} can be compared
With memcmp-with the same result as decimal_cmp () of the original
Decimals (not taking into account possible precision loss
Conversion ).

This binary format is as follows:
1. First the number is converted to have a requested precision and scale.
2. Every full dig_per_dec1 digits of intg part are stored in 4 bytes
As is
3. The first intg % dig_per_dec1 digits are stored in the specified CED
Number of bytes (enough bytes to store this number of digits-
See dig2bytes)
4. Same for frac-full decimal_digit_t's are stored as is,
The last frac % dig_per_dec1 digits-in the specified ced number of bytes.
5. If the number is negative-every byte is inversed.
5. The very first bit of the resulting byte array is inverted (because
Memcmp compares unsigned bytes, see property 2 above)

Example:

1234567890.1234

Internally is represented as 3 decimal_digit_t's

1 234567890 123400000

(Assuming we want a binary representation with precision = 14, scale = 4)
In hex it's

00-00-00-01 0d-fb-38-d2 07-5a-ef-40

Now, middle decimal_digit_t is full-It stores 9 decimal digits. It goes
Into binary representation as is:

...... 0d-fb-38-d2 ............

First decimal_digit_t has only one decimal digit. We can store one digit in
One byte, no need to waste four:

01 0d-fb-38-d2 ............

Now, last digit. It's 123400000. We can store 1234 in two bytes:

01 0d-fb-38-d2 04-d2

So, we 've packed 12 bytes number in 7 bytes.
And now we invert the highest bit to get the final result:

81 0d FB 38 D2 04 D2

And for-1234567890.1234 it wocould be

7E F2 04 37 2D FB 2D

This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or