# F.36.赛格
F.36.1.根本原因F.36.2.语法F.36.3.精确F.36.4.用法F.36.5.笔记F.36.6.信用
该模块实现了一种数据类型赛格
用于表示线段或浮点间隔。赛格
可以表示区间端点的不确定性,这使得它在表示实验室测量值时特别有用。
该模块被认为是“受信任的”,也就是说,它可以由拥有创造
当前数据库的权限。
# F.36.1.理由
测量的几何结构通常比数值连续体中的点更复杂。测量通常是连续统的一部分,有一些模糊的界限。由于不确定性和随机性,以及因为被测量的值自然可能是指示某些条件的区间,例如蛋白质稳定性的温度范围,所以测量结果以区间的形式出现。
仅凭常识,将此类数据存储为区间比成对的数字似乎更方便。实际上,在大多数应用程序中,它甚至更有效。
按照常识,界限的模糊性表明,使用传统的数字数据类型会导致一定程度的信息损失。考虑这个:你的仪器读6.50,然后把这个读入到数据库中。你去拿的时候会得到什么?观看:
test=> select 6.50 :: float8 as "pH";
pH
### F.36.2. Syntax
The external representation of an interval is formed using one or two floating-point numbers joined by the range operator (`..` or `...`). Alternatively, it can be specified as a center point plus or minus a deviation. Optional certainty indicators (`<`, `>` or `~`) can be stored as well. (Certainty indicators are ignored by all the built-in operators, however.) [Table F.27](seg.html#SEG-REPR-TABLE) gives an overview of allowed representations; [Table F.28](seg.html#SEG-INPUT-EXAMPLES) shows some examples.
In [Table F.27](seg.html#SEG-REPR-TABLE), *`x`*, *`y`*, and *`delta`* denote floating-point numbers. *`x`* and *`y`*, but not *`delta`*, can be preceded by a certainty indicator.
**Table F.27. `seg` External Representations**
| `*`x`*` | Single value (zero-length interval) |
|----------------------|----------------------------------------------------|
| `*`x`* .. *`y`*` | Interval from *`x`* to *`y`* |
|`*`x`* (+-) *`delta`*`|Interval from *`x`* - *`delta`* to *`x`* + *`delta`*|
| `*`x`* ..` | Open interval with lower bound *`x`* |
| `.. *`x`*` | Open interval with upper bound *`x`* |
**Table F.28. Examples of Valid `seg` Input**
| `5.0` | Creates a zero-length segment (a point, if you will) |
|-----------------|------------------------------------------------------------------------------------------------------------------------------|
| `~5.0` | Creates a zero-length segment and records `~` in the data. `~` is ignored by `seg` operations, but is preserved as a comment.|
| `<5.0` | Creates a point at 5.0. `<` is ignored but is preserved as a comment. |
| `>5.0` | Creates a point at 5.0. `>` is ignored but is preserved as a comment. |
| `5(+-)0.3` | Creates an interval `4.7 .. 5.3`. Note that the `(+-)` notation isn't preserved. |
| `50 .. ` | Everything that is greater than or equal to 50 |
| `.. 0` | Everything that is less than or equal to 0 |
|`1.5e-2 .. 2E-2 `| Creates an interval `0.015 .. 0.02` |
| `1 ... 2` | The same as `1...2`, or `1 .. 2`, or `1..2` (spaces around the range operator are ignored) |
Because the `...` operator is widely used in data sources, it is allowed as an alternative spelling of the `..` operator. Unfortunately, this creates a parsing ambiguity: it is not clear whether the upper bound in `0...23` is meant to be `23` or `0.23`. This is resolved by requiring at least one digit before the decimal point in all numbers in `seg` input.
As a sanity check, `seg` rejects intervals with the lower bound greater than the upper, for example `5 .. 2`.
### F.36.3. Precision
`seg` values are stored internally as pairs of 32-bit floating point numbers. This means that numbers with more than 7 significant digits will be truncated.
Numbers with 7 or fewer significant digits retain their original precision. That is, if your query returns 0.00, you will be sure that the trailing zeroes are not the artifacts of formatting: they reflect the precision of the original data. The number of leading zeroes does not affect precision: the value 0.0067 is considered to have just 2 significant digits.
### F.36.4. Usage
The `seg` module includes a GiST index operator class for `seg` values. The operators supported by the GiST operator class are shown in [Table F.29](seg.html#SEG-GIST-OPERATORS).
**Table F.29. Seg GiST Operators**
| Operator<br/><br/> Description |
|----------------------------------------------------------------------------------------------------------------------------------------|
| `seg` `<<` `seg` → `boolean`<br/><br/> Is the first `seg` entirely to the left of the second? [a, b] \<\< [c, d] is true if b \< c. |
| `seg` `>>` `seg` → `boolean`<br/><br/> Is the first `seg` entirely to the right of the second? [a, b] \>\> [c, d] is true if a \> d. |
|`seg` `&<` `seg` → `boolean`<br/><br/> Does the first `seg` not extend to the right of the second? [a, b] &\< [c, d] is true if b \<= d.|
|`seg` `&>` `seg` → `boolean`<br/><br/> Does the first `seg` not extend to the left of the second? [a, b] &\> [c, d] is true if a \>= c. |
| `seg` `=` `seg` → `boolean`<br/><br/> Are the two `seg`s equal? |
| `seg` `&&` `seg` → `boolean`<br/><br/> Do the two `seg`s overlap? |
| `seg` `@>` `seg` → `boolean`<br/><br/> Does the first `seg` contain the second? |
| `seg` `<@` `seg` → `boolean`<br/><br/> Is the first `seg` contained in the second? |
In addition to the above operators, the usual comparison operators shown in [Table 9.1](functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE) are available for type `seg`. These operators first compare (a) to (c), and if these are equal, compare (b) to (d). That results in reasonably good sorting in most cases, which is useful if you want to use ORDER BY with this type.
### F.36.5. Notes
For examples of usage, see the regression test `sql/seg.sql`.
The mechanism that converts `(+-)` to regular ranges isn't completely accurate in determining the number of significant digits for the boundaries. For example, it adds an extra digit to the lower boundary if the resulting interval includes a power of ten:
postgres=>选择'10(+-)1'::seg作为seg;赛格
# F.36.6.学分
原作者:小吉恩·塞尔科夫。<[selkovjr@mcs.anl.gov](邮寄至:selkovjr@mcs.anl.gov)>
,阿贡国家实验室数学和计算机科学部。
我首先要感谢乔·赫勒斯坦教授(https://dsf.berkeley.edu/jmh/ (opens new window))为了阐明要点的要点(http://gist.cs.berkeley.edu/ (opens new window))。我也感谢所有现在和过去的博士后开发者,感谢他们让我能够创造自己的世界,并在其中不受干扰地生活。我要感谢阿贡实验室和美国能源部多年来对我数据库研究的忠实支持。