Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement PostgreSQL floating-point number

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how the PostgreSQL floating point number is realized". In the daily operation, I believe that many people have doubts about how the PostgreSQL floating point number is realized. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "how the PostgreSQL floating point number is realized". Next, please follow the editor to study!

It is well known that computers store data in binary mode, and floating-point numbers may lose precision when serialized to binary (IEEE 754 standard). For database implementation, it will introduce a question, that is, how is it implemented in the database when comparing two floating-point numbers?

The following is a test script:

Testdb=# select 123.31::double precision > 123.45::double precision;; I. data structure

Floating point coding can be referred to Wikipedia, which simply consists of three parts, including symbol bits, significant digits and exponential bits. Among them, when the exponential bit is all 1 (binary 1), if the significant number is not all 0, then the number is not a number (represented by nan).

Second, source code interpretation

The comparison function of floating point number (double precision) is float8_cmp_internal, and the logic is relatively simple.

Among them, nan, that is, "not a numerical nan" introduced above.

/ * float8 {eq,ne,lt,le,gt,ge}-float8/float8 comparison operations * / intfloat8_cmp_internal (float8 a, float8 b) {/ * * We consider all NANs to be equal and larger than any non-NAN. This is * somewhat arbitrary; the important thing is to have a consistent sort * order. * / if (isnan (a)) {if (isnan (b)) return 0; / * NAN = NAN * / else return 1; / * NAN > non-NAN * /} else if (isnan (b)) {return-1; / * non-NAN

< NAN */ } else { if (a >

B) / / a > b, return 1 return 1; else if (a)

< b)//a < b,返回-1 return -1; else return 0;//否则,返回0 }} 在C语言中,浮点数不要比较相等或不等,但可以进行,>

=, 123.45::double precision

Tracking and analysis

(gdb) cContinuing.Breakpoint 1, float8_cmp_internal (isnan 123.31, bang 123.45) at float.c:10561056 if (isnan (a))

View data in memory (8 bytes, displayed as a single byte b)

(gdb) x gdb 8b & a0x7ffcd2cac728: 0xa4 0x70 0x3d 0x0a 0xd7 0xd3 0x5e 0x40 (gdb) x Compal 8b & b0x7ffcd2cac720: 0xcd 0xcc 0xcc 0xcc 0xcc 0xdc 0x5e 0x40 (gdb)

At the same time, we use C language to print 123.31 and 123.45 binary codes as a comparison.

[xdb@localhost source] $cat double_test.c # include int main () {double D1 = 123.31; double D2 = 123.45; printf ("D1:% llx\ n", * ((long *) & D1)); printf ("D2:% llx\ n", * ((long *) & D2));} [xdb@localhost source] $gcc double_test.c-o dt [xdb@localhost source] $. / dt D1: 405ed3d70a3d70a4 D2: 405edccccccccccd

The output value is consistent with the memory value in the trace analysis.

At this point, the study of "how to achieve PostgreSQL floating-point numbers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report