This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris-website.git
The following commit(s) were added to refs/heads/master by this push:
new 758b4ae9a87 [doc] FLOATING-POINT doc (#2824)
758b4ae9a87 is described below
commit 758b4ae9a87c1596d6a298fdec7a375a1bb6d7c0
Author: Mryange <[email protected]>
AuthorDate: Mon Sep 1 14:06:34 2025 +0800
[doc] FLOATING-POINT doc (#2824)
## Versions
- [x] dev
- [ ] 3.0
- [ ] 2.1
- [ ] 2.0
## Languages
- [x] Chinese
- [x] English
## Docs Checklist
- [x] Checked by AI
- [ ] Test Cases Built
---
.../basic-element/sql-data-types/numeric/DOUBLE.md | 49 ------
.../basic-element/sql-data-types/numeric/FLOAT.md | 49 ------
.../sql-data-types/numeric/FLOATING-POINT.md | 191 +++++++++++++++++++++
.../basic-element/sql-data-types/numeric/DOUBLE.md | 50 ------
.../basic-element/sql-data-types/numeric/FLOAT.md | 50 ------
.../sql-data-types/numeric/FLOATING-POINT.md | 191 +++++++++++++++++++++
sidebars.json | 5 +-
7 files changed, 384 insertions(+), 201 deletions(-)
diff --git a/docs/sql-manual/basic-element/sql-data-types/numeric/DOUBLE.md
b/docs/sql-manual/basic-element/sql-data-types/numeric/DOUBLE.md
deleted file mode 100644
index d22347dec36..00000000000
--- a/docs/sql-manual/basic-element/sql-data-types/numeric/DOUBLE.md
+++ /dev/null
@@ -1,49 +0,0 @@
----
-{
- "title": "DOUBLE",
- "language": "en"
-}
----
-
-## DOUBLE
-## Description
- DOUBLE
- 8-byte double-precision floating-point number.
-
-## Value Range
-
-Doris uses IEEE-754 double-precision floating-point numbers, the value range
is:
-
-- -∞ (-Infinity)
-- [-1.79769E+308, -2.225E-307]
-- 0
-- [+2.225E-307, +1.79769E+308]
-- +∞ (+Infinity)
-- NaN (Not a Number)
-
-For details, refer to [C++ double
type](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)
and [Wikipedia Double-precision floating-point
format](https://en.wikipedia.org/wiki/Double-precision_floating-point_format).
-
-## Examples
-
-```sql
-create table double_test(f1 int, f2 double) properties("replication_num"="1");
-
-insert into double_test values (0, "-Infinity"), (1, -1.79769E+308), (2,
-2.225E-307), (3, 0), (4, +2.225E-307), (5, +1.79769E+308), (6, "+Infinity"),
(7, "NaN");
-
-select * from double_test order by f1;
-+------+---------------+
-| f1 | f2 |
-+------+---------------+
-| 0 | -Infinity |
-| 1 | -1.79769e+308 |
-| 2 | -2.225e-307 |
-| 3 | 0 |
-| 4 | 2.225e-307 |
-| 5 | 1.79769e+308 |
-| 6 | Infinity |
-| 7 | NaN |
-+------+---------------+
-```
-
-### keywords
-DOUBLE
diff --git a/docs/sql-manual/basic-element/sql-data-types/numeric/FLOAT.md
b/docs/sql-manual/basic-element/sql-data-types/numeric/FLOAT.md
deleted file mode 100644
index ecfd3b46665..00000000000
--- a/docs/sql-manual/basic-element/sql-data-types/numeric/FLOAT.md
+++ /dev/null
@@ -1,49 +0,0 @@
----
-{
- "title": "FLOAT",
- "language": "en"
-}
----
-
-## FLOAT
-## Description
- FLOAT
- 4-byte single-precision floating-point number.
-
-## Value Range
-
-Doris uses IEEE-754 single-precision floating-point numbers, the value range
is:
-
-- -∞ (-Infinity)
-- [-3.402E+38, -1.175E-37]
-- 0
-- [1.175E-37, 3.402E+38]
-- +∞ (+Infinity)
-- NaN (Not a Number)
-
-For details, refer to [C++ float
type](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)
and [Wikipedia Single-precision floating-point
format](https://en.wikipedia.org/wiki/Single-precision_floating-point_format).
-
-## Examples
-
-```sql
-create table float_test(f1 int, f2 float) properties("replication_num"="1");
-
-insert into float_test values(0, "-Infinity"), (1, -3.402E+38), (2,
-1.175E-37), (3, 0), (4, +1.175E-37), (5, +3.402E+38), (6, "+Infinity"), (7,
"NaN");
-
-select * from float_test order by f1;
-+------+------------+
-| f1 | f2 |
-+------+------------+
-| 0 | -Infinity |
-| 1 | -3.402e+38 |
-| 2 | -1.175e-37 |
-| 3 | 0 |
-| 4 | 1.175e-37 |
-| 5 | 3.402e+38 |
-| 6 | Infinity |
-| 7 | NaN |
-+------+------------+
-```
-
-### keywords
-FLOAT
diff --git
a/docs/sql-manual/basic-element/sql-data-types/numeric/FLOATING-POINT.md
b/docs/sql-manual/basic-element/sql-data-types/numeric/FLOATING-POINT.md
new file mode 100644
index 00000000000..ffc817680f5
--- /dev/null
+++ b/docs/sql-manual/basic-element/sql-data-types/numeric/FLOATING-POINT.md
@@ -0,0 +1,191 @@
+---
+{
+ "title": "Floating-Point Types (FLOAT and DOUBLE)",
+ "language": "en"
+}
+---
+
+## Description
+
+Doris provides two floating-point data types: `FLOAT` and `DOUBLE`. These are
variable-precision numeric types that follow the IEEE 754 standard for
floating-point arithmetic.
+
+| Type | Alias | Storage Size | Description |
+|------|------|----------|------|
+| FLOAT | FLOAT4, REAL | 4 bytes | Single-precision floating-point |
+| DOUBLE | FLOAT8, DOUBLE PRECISION | 8 bytes | Double-precision
floating-point |
+
+## Value Range
+
+### FLOAT
+
+Doris uses IEEE-754 single-precision floating-point numbers, with a value
range of:
+
+- -∞ (-Infinity)
+- [-3.402E+38, -1.175E-37]
+- 0
+- [1.175E-37, 3.402E+38]
+- +∞ (+Infinity)
+- NaN (Not a Number)
+
+For more details, see [C++ float
type](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)
and [Wikipedia Single-precision floating-point
format](https://en.wikipedia.org/wiki/Single-precision_floating-point_format).
+
+### DOUBLE
+
+Doris uses IEEE-754 double-precision floating-point numbers, with a value
range of:
+
+- -∞ (-Infinity)
+- [-1.79769E+308, -2.225E-307]
+- 0
+- [+2.225E-307, +1.79769E+308]
+- +∞ (+Infinity)
+- NaN (Not a Number)
+
+For more details, see [C++ double
type](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)
and [Wikipedia Double-precision floating-point
format](https://en.wikipedia.org/wiki/Double-precision_floating-point_format).
+
+## Special Values
+
+In addition to regular numeric values, floating-point types have several
special values that conform to the IEEE 754 standard:
+
+- `Infinity` or `Inf`: Positive infinity
+- `-Infinity` or `-Inf`: Negative infinity
+- `NaN`: Not a Number
+
+These special values can be generated through CAST conversions:
+
+```sql
+mysql> select cast('NaN' as double), cast('inf' as double), cast('-Infinity'
as double);
++-----------------------+-----------------------+-----------------------------+
+| cast('NaN' as double) | cast('inf' as double) | cast('-Infinity' as double) |
++-----------------------+-----------------------+-----------------------------+
+| NaN | Infinity | -Infinity |
++-----------------------+-----------------------+-----------------------------+
+```
+
+Floating-point numbers also have a non-intuitive property: there are two
different zero values, `+0` and `-0`.
+While they are considered equal in most contexts, they differ in their sign
bit:
+
+```sql
+mysql> select cast('+0.0' as double), cast('-0.0' as double);
++------------------------+------------------------+
+| cast('+0.0' as double) | cast('-0.0' as double) |
++------------------------+------------------------+
+| 0 | -0 |
++------------------------+------------------------+
+```
+
+## Floating-Point Operations
+
+### Arithmetic Operations
+
+Doris floating-point numbers support common arithmetic operations like
addition, subtraction, multiplication, and division.
+
+It's important to note that Doris does not fully follow the IEEE 754 standard
when handling division by 0 with floating-point numbers.
+
+Doris follows PostgreSQL's implementation in this regard, returning SQL NULL
instead of generating special values when dividing by 0:
+
+| Expression | PostgreSQL | IEEE 754 | Doris |
+|--------|------------|----------|-------|
+| 1.0 / 0.0 | Error | Infinity | NULL |
+| 0.0 / 0.0 | Error | NaN | NULL |
+| -1.0 / 0.0 | Error | -Infinity | NULL |
+| 'Infinity' / 'Infinity' | NaN | NaN | NaN |
+| 1.0 / 'Infinity' | 0.0 | 0.0 | 0 |
+| 'Infinity' - 'Infinity' | NaN | NaN | NaN |
+| 'Infinity' - 1.0 | Infinity | Infinity | Infinity |
+
+### Comparison Operations
+
+The IEEE standard defines floating-point comparisons that differ from typical
integer comparisons in important ways. For example, negative zero and positive
zero are considered equal, and any NaN value is not equal to any other value
(including itself). All finite floating-point numbers are strictly less than +∞
and strictly greater than -∞.
+
+To ensure consistency and predictability of results, Doris handles NaN
differently from the IEEE standard.
+In Doris, NaN is considered greater than all other values (including
Infinity), and NaN equals NaN.
+
+```sql
+mysql> select * from sort_float order by d;
++------+-----------+
+| id | d |
++------+-----------+
+| 5 | -Infinity |
+| 2 | -123 |
+| 1 | 123 |
+| 4 | Infinity |
+| 8 | NaN |
+| 9 | NaN |
++------+-----------+
+
+mysql> select
+ cast('Nan' as double) = cast('Nan' as double) ,
+ cast('Nan' as double) > cast('Inf' as double) ,
+ cast('Nan' as double) > cast('123456.789' as double);
++-----------------------------------------------+-----------------------------------------------+------------------------------------------------------+
+| cast('Nan' as double) = cast('Nan' as double) | cast('Nan' as double) >
cast('Inf' as double) | cast('Nan' as double) > cast('123456.789' as double) |
++-----------------------------------------------+-----------------------------------------------+------------------------------------------------------+
+| 1 |
1 | 1 |
++-----------------------------------------------+-----------------------------------------------+------------------------------------------------------+
+```
+
+## Floating-Point Precision Issues
+
+### Approximate Values and Precision Loss
+
+Floating-point numbers are inherently approximate representations. This means
that certain decimal values cannot be stored exactly in the binary
representation of floating-point numbers and are instead stored as
approximations. As a result, small discrepancies may occur during storage and
retrieval.
+
+For example:
+
+```sql
+mysql> SELECT CAST(1.3 AS FLOAT) - CAST(0.7 AS FLOAT) = CAST(0.6 AS FLOAT);
++--------------------------------------------------------------+
+| CAST(1.3 AS FLOAT) - CAST(0.7 AS FLOAT) = CAST(0.6 AS FLOAT) |
++--------------------------------------------------------------+
+| 0 |
++--------------------------------------------------------------+
+```
+
+Due to floating-point representation errors, this may not evaluate to `TRUE`
as expected.
+
+### Operations Don't Follow Associative Law
+
+Due to precision limitations in floating-point operations, the computational
properties of floating-point numbers differ from theoretical mathematical
operations. Floating-point addition and multiplication do not strictly follow
the [associative and distributive
laws](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems).
+
+This leads to an important consequence: different computation orders may
produce slightly different results.
+Since Doris uses an MPP architecture and cannot guarantee the exact order of
data processing, calculations involving floating-point numbers may produce
slightly different results in different executions, even with identical input
data.
+
+#### Aggregate Functions
+
+Performing aggregate functions on floating-point values may accumulate errors,
especially when dealing with large datasets. When data contains extremely large
or small values, these errors can be further amplified.
+Due to the indeterminate calculation order, running the same aggregate
function multiple times may yield different results when the data contains
extreme values.
+
+#### Join Operations
+
+Similar to aggregate functions, it is not recommended to perform table joins
on floating-point columns. Due to precision issues with floating-point numbers,
two theoretically equal values might have slightly different internal
representations, causing matches to fail.
+
+### Floating-Point Output
+
+When floating-point numbers are converted to strings, Doris follows these
precision rules:
+- Single-precision floating-point numbers (FLOAT) guarantee at least 7
significant digits
+- Double-precision floating-point numbers (DOUBLE) guarantee at least 16
significant digits
+Note that floating-point output may use scientific notation, so the length of
a floating-point string representation is not necessarily equal to its number
of significant digits:
+```sql
+mysql> select cast('1234567' as float) , cast('12345678' as float);
++--------------------------+---------------------------+
+| cast('1234567' as float) | cast('12345678' as float) |
++--------------------------+---------------------------+
+| 1234567 | 1.234568e+07 |
++--------------------------+---------------------------+
+```
+
+## Best Practices
+
+1. **Choose the appropriate data type**: For financial calculations or other
scenarios requiring exact numeric values, use the `DECIMAL` type instead of
floating-point types.
+
+2. **Be cautious with equality comparisons**: Avoid directly comparing whether
two floating-point values are equal, especially in JOIN operations.
+
+3. **Be careful with string conversions**: Converting floating-point numbers
to strings and back may introduce additional precision loss.
+
+4. **Understand platform differences**: Different database systems may have
subtle differences in handling floating-point operations, especially when
dealing with special cases like NaN and Infinity (although most database
systems broadly follow the IEEE standard).
+
+5. **Round results appropriately for display**: When displaying floating-point
calculation results, consider appropriate rounding to reduce precision issues
for users.
+
+## Keywords
+
+FLOAT, FLOAT4, REAL, DOUBLE, DOUBLE PRECISION, FLOAT8, floating-point
diff --git
a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/DOUBLE.md
b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/DOUBLE.md
deleted file mode 100644
index 12eedf87505..00000000000
---
a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/DOUBLE.md
+++ /dev/null
@@ -1,50 +0,0 @@
----
-{
- "title": "DOUBLE",
- "language": "zh-CN"
-}
----
-
-## DOUBLE
-## 描述
- DOUBLE
- 8字节双精度浮点数。
-
-## 取值范围
-
-Doris使用IEEE-754双精度浮点数,取值范围:
-
-- -∞(负无穷大, -Infinity)
-- [-1.79769E+308, -2.225E-307]
-- 0
-- [+2.225E-307, +1.79769E+308]
-- +∞(正无穷大, +Infinity)
-- NaN(Not a Number)
-
-详情参见[C++ double
类型](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)和[Wikipedia双精度浮点数](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)。
-
-## 示例
-
-```sql
-create table double_test(f1 int, f2 double) properties("replication_num"="1");
-
-insert into double_test values (0, "-Infinity"), (1, -1.79769E+308), (2,
-2.225E-307), (3, 0), (4, +2.225E-307), (5, +1.79769E+308), (6, "+Infinity"),
(7, "NaN");
-
-select * from double_test order by f1;
-+------+---------------+
-| f1 | f2 |
-+------+---------------+
-| 0 | -Infinity |
-| 1 | -1.79769e+308 |
-| 2 | -2.225e-307 |
-| 3 | 0 |
-| 4 | 2.225e-307 |
-| 5 | 1.79769e+308 |
-| 6 | Infinity |
-| 7 | NaN |
-+------+---------------+
-```
-
-### keywords
-
- DOUBLE
diff --git
a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/FLOAT.md
b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/FLOAT.md
deleted file mode 100644
index 5b9a49e894f..00000000000
---
a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/FLOAT.md
+++ /dev/null
@@ -1,50 +0,0 @@
----
-{
- "title": "FLOAT",
- "language": "zh-CN"
-}
----
-
-## FLOAT
-## 描述
- FLOAT
- 4字节单精度浮点数。
-
-## 取值范围
-
-Doris使用IEEE-754单精度浮点数,取值范围:
-
-- -∞(负无穷大, -Infinity)
-- [-3.402E+38, -1.175E-37]
-- 0
-- [1.175E-37, 3.402E+38]
-- +∞(正无穷大, +Infinity)
-- NaN(Not a Number)
-
-详情参见[C++ float
类型](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)和[Wikipedia单精度浮点数](https://en.wikipedia.org/wiki/Single-precision_floating-point_format)。
-
-## 示例
-
-```sql
-create table float_test(f1 int, f2 float) properties("replication_num"="1");
-
-insert into float_test values(0, "-Infinity"), (1, -3.402E+38), (2,
-1.175E-37), (3, 0), (4, +1.175E-37), (5, +3.402E+38), (6, "+Infinity"), (7,
"NaN");
-
-select * from float_test order by f1;
-+------+------------+
-| f1 | f2 |
-+------+------------+
-| 0 | -Infinity |
-| 1 | -3.402e+38 |
-| 2 | -1.175e-37 |
-| 3 | 0 |
-| 4 | 1.175e-37 |
-| 5 | 3.402e+38 |
-| 6 | Infinity |
-| 7 | NaN |
-+------+------------+
-```
-
-### keywords
-
- FLOAT
diff --git
a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/FLOATING-POINT.md
b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/FLOATING-POINT.md
new file mode 100644
index 00000000000..b52d0bed8de
--- /dev/null
+++
b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/sql-data-types/numeric/FLOATING-POINT.md
@@ -0,0 +1,191 @@
+---
+{
+ "title": "浮点类型 (FLOAT 和 DOUBLE)",
+ "language": "zh-CN"
+}
+---
+
+## 描述
+
+Doris 提供了两种浮点数据类型:`FLOAT` 和 `DOUBLE`。这些是可变精度的数值类型,遵循 IEEE 754 浮点算术标准。
+
+| 类型 | 别名 | 存储空间 | 描述 |
+|------|------|----------|------|
+| FLOAT | FLOAT4, REAL | 4 字节 | 单精度浮点数 |
+| DOUBLE | FLOAT8, DOUBLE PRECISION | 8 字节 | 双精度浮点数 |
+
+## 取值范围
+
+### FLOAT
+
+Doris 使用 IEEE-754 单精度浮点数,取值范围为:
+
+- -∞ (-Infinity)
+- [-3.402E+38, -1.175E-37]
+- 0
+- [1.175E-37, 3.402E+38]
+- +∞ (+Infinity)
+- NaN (不是数字)
+
+详情请参阅 [C++ float
类型](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)
和 [Wikipedia
单精度浮点格式](https://en.wikipedia.org/wiki/Single-precision_floating-point_format)。
+
+### DOUBLE
+
+Doris 使用 IEEE-754 双精度浮点数,取值范围为:
+
+- -∞ (-Infinity)
+- [-1.79769E+308, -2.225E-307]
+- 0
+- [+2.225E-307, +1.79769E+308]
+- +∞ (+Infinity)
+- NaN (不是数字)
+
+详情请参阅 [C++ double
类型](https://en.cppreference.com/w/cpp/language/types.html#Standard_floating-point_types)
和 [Wikipedia
双精度浮点格式](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)。
+
+## 特殊值
+
+除了普通的数值外,浮点类型还有几个特殊值,这些值符合 IEEE 754 标准:
+
+- `Infinity` 或 `Inf`:正无穷大
+- `-Infinity` 或 `-Inf`:负无穷大
+- `NaN`:不是数字(Not a Number)
+
+可以通过 CAST 转换来生成这些特殊值:
+
+```sql
+mysql> select cast('NaN' as double), cast('inf' as double), cast('-Infinity'
as double);
++-----------------------+-----------------------+-----------------------------+
+| cast('NaN' as double) | cast('inf' as double) | cast('-Infinity' as double) |
++-----------------------+-----------------------+-----------------------------+
+| NaN | Infinity | -Infinity |
++-----------------------+-----------------------+-----------------------------+
+```
+
+浮点数还有一个不太直观的特性:存在两种不同的零值,即 `+0` 和 `-0`。
+虽然它们在大多数情况下被视为相等,但它们的符号位不同:
+
+```sql
+mysql> select cast('+0.0' as double), cast('-0.0' as double);
++------------------------+------------------------+
+| cast('+0.0' as double) | cast('-0.0' as double) |
++------------------------+------------------------+
+| 0 | -0 |
++------------------------+------------------------+
+```
+
+## 浮点数运算
+
+### 算术运算
+
+Doris 的浮点数支持常见的加减乘除等算术运算。
+
+需要特别注意的是,Doris 在处理浮点数除以 0 的情况时,并不完全遵循 IEEE 754 标准。
+
+Doris 在这方面参考了 PostgreSQL 的实现,当除以 0 时不会生成特殊值,而是返回 SQL NULL:
+
+| 表达式 | PostgreSQL | IEEE 754 | Doris |
+|--------|------------|----------|-------|
+| 1.0 / 0.0 | 错误 | Infinity | NULL |
+| 0.0 / 0.0 | 错误 | NaN | NULL |
+| -1.0 / 0.0 | 错误 | -Infinity | NULL |
+| 'Infinity' / 'Infinity' | NaN | NaN | NaN |
+| 1.0 / 'Infinity' | 0.0 | 0.0 | 0 |
+| 'Infinity' - 'Infinity' | NaN | NaN | NaN |
+| 'Infinity' - 1.0 | Infinity | Infinity | Infinity |
+
+### 比较运算
+
+IEEE 标准定义的浮点数比较与通常的整数比较有一些重要区别。例如,负零和正零被视为相等,而任何 NaN
值与任何其他值(包括它自身)比较时都不相等。所有有限浮点数都严格小于 +∞,严格大于 -∞。
+
+为了确保结果的一致性和可预测性,Doris 对 NaN 的处理与 IEEE 标准有所不同。
+在 Doris 中,NaN 被视为大于所有其他值(包括 Infinity), NaN 等于 NaN。
+
+```sql
+mysql> select * from sort_float order by d;
++------+-----------+
+| id | d |
++------+-----------+
+| 5 | -Infinity |
+| 2 | -123 |
+| 1 | 123 |
+| 4 | Infinity |
+| 8 | NaN |
+| 9 | NaN |
++------+-----------+
+
+mysql> select
+ cast('Nan' as double) = cast('Nan' as double) ,
+ cast('Nan' as double) > cast('Inf' as double) ,
+ cast('Nan' as double) > cast('123456.789' as double);
++-----------------------------------------------+-----------------------------------------------+------------------------------------------------------+
+| cast('Nan' as double) = cast('Nan' as double) | cast('Nan' as double) >
cast('Inf' as double) | cast('Nan' as double) > cast('123456.789' as double) |
++-----------------------------------------------+-----------------------------------------------+------------------------------------------------------+
+| 1 |
1 | 1 |
++-----------------------------------------------+-----------------------------------------------+------------------------------------------------------+
+```
+
+## 浮点精度问题
+
+### 近似值和精度损失
+
+浮点数本质上是一种近似表示形式。这意味着某些十进制值无法在浮点数的二进制表示中被精确存储,只能以近似值的方式保存。因此,在存储和检索过程中可能会出现微小的差异。
+
+例如:
+
+```sql
+mysql> SELECT CAST(1.3 AS FLOAT) - CAST(0.7 AS FLOAT) = CAST(0.6 AS FLOAT);
++--------------------------------------------------------------+
+| CAST(1.3 AS FLOAT) - CAST(0.7 AS FLOAT) = CAST(0.6 AS FLOAT) |
++--------------------------------------------------------------+
+| 0 |
++--------------------------------------------------------------+
+```
+
+由于浮点表示误差,这可能不会按预期评估为 `TRUE`。
+
+### 运算不满足结合律
+
+由于浮点运算中的精度限制,浮点数的计算特性与理论数学运算有所差异。浮点加法和乘法不严格遵循[结合律和分配律](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems)。
+
+这就导致了一个重要后果:计算顺序的不同可能会产生略微不同的结果。
+由于 Doris 采用 MPP 架构,无法保证数据的精确处理顺序,因此即使输入数据完全相同,涉及浮点数的计算可能在不同执行中产生轻微不同的结果。
+
+#### 聚合函数
+
+对浮点值执行聚合函数可能会积累误差,特别是在处理大规模数据集时。当数据中包含极大或极小的值时,这种误差会被进一步放大。
+由于计算顺序不确定,当数据中存在极端值时,多次执行相同的聚合函数可能会得到不同的结果。
+
+#### Join 操作
+
+与聚合函数类似,不建议在浮点列上进行表连接操作。由于浮点数的精度问题,两个理论上相等的值可能在内部表示上略有差异,从而导致匹配失败。
+
+### 浮点数的输出
+
+当浮点数转换为字符串时,Doris 遵循以下精度规则:
+- 单精度浮点数(FLOAT)保证至少 7 位有效数字
+- 双精度浮点数(DOUBLE)保证至少 16 位有效数字
+需要注意的是,浮点数输出可能采用科学计数法表示,因此浮点数字符串表示的长度不一定等于其有效位数:
+```sql
+mysql> select cast('1234567' as float) , cast('12345678' as float);
++--------------------------+---------------------------+
+| cast('1234567' as float) | cast('12345678' as float) |
++--------------------------+---------------------------+
+| 1234567 | 1.234568e+07 |
++--------------------------+---------------------------+
+```
+
+## 最佳实践
+
+1. **选择合适的数据类型**:对于财务计算或其他需要精确数值的场景,应使用 `DECIMAL` 类型而非浮点类型。
+
+2. **谨慎进行相等比较**:避免直接比较两个浮点值是否相等,尤其是在 JOIN 操作中。
+
+3. **小心处理字符串转换**:将浮点数转换为字符串再转回浮点数可能会引入额外的精度损失。
+
+4. **了解平台差异**:不同的数据库系统在处理浮点运算时可能存在细微差别,特别是在处理 NaN 和 Infinity
等特殊情况时(不过大多数数据库系统都基本遵循 IEEE 标准)。
+
+5. **展示结果时进行适当舍入**:在展示浮点计算结果时,考虑进行适当的舍入处理,以减少精度问题对用户的困扰。
+
+## 关键字
+
+FLOAT, FLOAT4, REAL, DOUBLE, DOUBLE PRECISION, FLOAT8, 浮点
diff --git a/sidebars.json b/sidebars.json
index 18e12ab7482..2f8dbca660b 100644
--- a/sidebars.json
+++ b/sidebars.json
@@ -987,9 +987,8 @@
"sql-manual/basic-element/sql-data-types/numeric/INT",
"sql-manual/basic-element/sql-data-types/numeric/BIGINT",
"sql-manual/basic-element/sql-data-types/numeric/LARGEINT",
-
"sql-manual/basic-element/sql-data-types/numeric/FLOAT",
-
"sql-manual/basic-element/sql-data-types/numeric/DOUBLE",
-
"sql-manual/basic-element/sql-data-types/numeric/DECIMAL"
+
"sql-manual/basic-element/sql-data-types/numeric/DECIMAL",
+
"sql-manual/basic-element/sql-data-types/numeric/FLOATING-POINT"
]
},
{
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]