Skip to content

Commit

Permalink
fix: fix inconsistency between floating-point number's storage (#13)
Browse files Browse the repository at this point in the history
# 问题:浮点数的存储和输出格式不一致导致无法精确查找
### 描述:现有TDB的float存储时未作截断,打印时截断至两位小数导致无法精确查找。因此取消打印时的位数截断。

![image](https://github.com/THSS-DB/TDB/assets/74061089/4c2d9104-7de0-4d85-a4b7-c10bc7003eb5)
#### 分析:直接打印为 %f 不会因为缓冲区导致截断
float的阶码为8位,尾数为23位。
规格化最大正数为:2^24-1)x2^(127-23) = (2^24-1)x2^104,即 3.4028235E+38
规格化最小正数为:1x2^(-126),即1.1754943E-38
非规格化最小正数为:2^(-23)x2^(-126)=2^(-149),即1.401298E-45
均不会超过分配的256字节的缓冲区buf,因此可以完整输出。

Co-authored-by: thu <[email protected]>
Co-authored-by: YangCaiyin <[email protected]>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent df87a42 commit a43bd5f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion deps/common/lang/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ char *substr(const char *s, int n1, int n2)
std::string double_to_str(double v)
{
char buf[256];
snprintf(buf, sizeof(buf), "%.2f", v);
//snprintf(buf, sizeof(buf), "%.2f", v);
// 为了实现float的精确查找,不再截断float的小数位
snprintf(buf, sizeof(buf), "%f", v);
size_t len = strlen(buf);
while (buf[len - 1] == '0') {
len--;
Expand Down

0 comments on commit a43bd5f

Please sign in to comment.