关于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

关于decimal非常浅显的学习与整理的更多相关文章

  1. Mysql学习笔记整理手册

    目录 (1) str_to_date (2) 递归查询 (3) 排序问题 (4) 条件函数 (5) 列转行函数 (6) find_int_set (7) 类型转换函数 (8) 合并更新 继上一篇博客& ...

  2. UWP学习目录整理

    UWP学习目录整理 0x00 可以忽略的废话 10月6号靠着半听半猜和文字直播的补充看完了微软的秋季新品发布会,信仰充值成功,对UWP的开发十分感兴趣,打算后面找时间学习一下.谁想到学习的欲望越来越强 ...

  3. iOS 开发学习资料整理(持续更新)

      “如果说我看得比别人远些,那是因为我站在巨人们的肩膀上.” ---牛顿   iOS及Mac开源项目和学习资料[超级全面] http://www.kancloud.cn/digest/ios-mac ...

  4. DBN 入门学习资料整理

    建议按序阅读 1.Deep Learning 概述 Deep Learning(深度学习)学习笔记整理系列 : http://blog.csdn.net/zouxy09/article/details ...

  5. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  6. iOS 学习资料整理

    iOS学习资料整理 https://github.com/NunchakusHuang/trip-to-iOS 很好的个人博客 http://www.cnblogs.com/ygm900/ 开发笔记 ...

  7. Deep Learning(深度学习)学习笔记整理系列之(五)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  8. Git学习资料整理

    Git作为一个优秀的版本控制系统,是我们开发人员必须要学会使用的一个工具,接触git一年多以来,也看了不少相关资料,今天把我所看过的一些学习资源整理一下. Git入门当然首推廖雪峰廖老师的教程:Git ...

  9. Deep Learning(深度学习)学习笔记整理系列之(八)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  10. Deep Learning(深度学习)学习笔记整理系列之(七)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

随机推荐

  1. ubuntu upgrade problem

    出现问题: W: Failed to fetch bzip2:/var/lib/apt/lists/partial/ppa.launchpad.net_webupd8team_java_ubuntu_ ...

  2. zabbix 利用脚本发邮件(mail)

    # 源码安装mailx tar jxvf mailx-12.3.tar.bz2 make && make install UCBINSTALL=/usr/bin/install #yu ...

  3. C#新鲜面试题出炉(2024)

    总所周知  C#这门语言 没有Java的八股文,所以面试题一般都是问的业务, 那么对于新手来讲,最起码也要会一些基础性问题, 以下就是包含C# 和sqlserver几个常见的面试题   1) Dele ...

  4. kubernetes之部署springboot项目jar项目(三)

    kubernetes之部署war项目(三) k8s系列 源自我工作上的实际场景,记录于此. k8s部署springboot项目 假设我的jar项目是:myjar.jar 用到的环境 centos 7. ...

  5. 如何通过appuploader把ipa文件上传到App Store教程步骤​

    iOS APP上架App Store其中一个步骤就是要把ipa文件上传到App Store!​ 下面进行步骤介绍!​ 利用Appuploader这个软件,可以在Windows.Linux或Mac系统中 ...

  6. 火山引擎DataTester:AB测试技术揭秘及应用分享

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群   在全球软件工程创新峰会上,火山引擎DataTester 研发负责人韩云飞围绕"AB测试:让数据与业务 ...

  7. jQuery模糊匹配checkbox全选 value实现checkbox部分或全部全选

    本文章总结jQuery实现checkbox三种情况的全选功能 第一种:等值全选,也称name的等值全选,通过checkbox的名称name实现. 第二种:模糊全选,也称id模糊全选,通过checkbo ...

  8. Solon 编译打包怎样保持参数名不变?

    1.Java 项目 Java maven 项目 <plugin> <groupId>org.apache.maven.plugins</groupId> <a ...

  9. VS 2019 目标框架中看不到 Net Core 3.X

    VS 2019 目标框架中没有 .NET Core 3.X..Net 5.0 https://dotnet.microsoft.com/download/dotnet-core/3.0 Visual ...

  10. EXECL函数

    1 COUNTIF 对比两列数据,有相同的即计为1 找一列空白列,输入=COUNTIF(范围,条件),按回车,然后再点击表格右下角的"+" 就可以拉动持续执行这个函数 2 CONC ...