From 325f678c533146ca77d5378a91bd2526c475200c Mon Sep 17 00:00:00 2001 From: Ryosei Sato <132046562+sary8@users.noreply.github.com> Date: Wed, 16 Sep 2026 07:54:09 +0900 Subject: [PATCH 1/2] functions-and-operators: fix the mismatched quote in the sql_mode example The string in the SET sql_mode example opens with an apostrophe and closes with a backtick, so the statement fails when a reader copies it. --- functions-and-operators/precision-math.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions-and-operators/precision-math.md b/functions-and-operators/precision-math.md index e3916a1b75b92..53d4cede78adc 100644 --- a/functions-and-operators/precision-math.md +++ b/functions-and-operators/precision-math.md @@ -68,7 +68,7 @@ If a numeric expression contains strings, the strings are converted to double-pr Inserts into numeric columns are affected by the SQL mode. The following discussions mention strict mode and `ERROR_FOR_DIVISION_BY_ZERO`. To turn on all the restrictions, you can simply use the `TRADITIONAL` mode, which includes both strict mode values and `ERROR_FOR_DIVISION_BY_ZERO`: ```sql -SET sql_mode = 'TRADITIONAL`; +SET sql_mode = 'TRADITIONAL'; ``` If a number is inserted into an exact type column (DECIMAL or integer), it is inserted with its exact value if it is within the column range. For this number: From 53456aec09b6c7e18e4a0915e0c60381b1da94e4 Mon Sep 17 00:00:00 2001 From: Ryosei Sato <132046562+sary8@users.noreply.github.com> Date: Wed, 16 Sep 2026 07:54:40 +0900 Subject: [PATCH 2/2] functions-and-operators: correct the approximate-value rounding rule The page says that ROUND() on approximate-value numbers behaves differently in TiDB than in MySQL, and the example shows ROUND(25E-1) returning 3. pingcap/tidb#21324 changed approximate-value rounding to "round to nearest even" to match MySQL. It shipped in v4.0.11 through the backport pingcap/tidb#21628, and in v5.0.0 through master, so the example now returns 2. The stale sentence also contradicted the opening line of the page, which states that precision math in TiDB is consistent with MySQL. Move the example out of the list, since its first column illustrates the exact-value rule and its second column the approximate-value one. --- functions-and-operators/precision-math.md | 26 +++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/functions-and-operators/precision-math.md b/functions-and-operators/precision-math.md index 53d4cede78adc..e0c5457efdcdf 100644 --- a/functions-and-operators/precision-math.md +++ b/functions-and-operators/precision-math.md @@ -108,17 +108,21 @@ The following results are returned in different SQL modes: The result of the `ROUND()` function depends on whether its argument is exact or approximate: - For exact-value numbers, the `ROUND()` function uses the "round half up" rule. -- For approximate-value numbers, the results in TiDB differs from that in MySQL: - - ```sql - TiDB > SELECT ROUND(2.5), ROUND(25E-1); - +------------+--------------+ - | ROUND(2.5) | ROUND(25E-1) | - +------------+--------------+ - | 3 | 3 | - +------------+--------------+ - 1 row in set (0.00 sec) - ``` +- For approximate-value numbers, the `ROUND()` function uses the "round to nearest even" rule, which rounds a value exactly halfway between two integers to the nearest even integer. + +The following example shows both rules. `2.5` is an exact-value literal and `25E-1` is the same number written as an approximate value: + +```sql +TiDB > SELECT ROUND(2.5), ROUND(25E-1); ++------------+--------------+ +| ROUND(2.5) | ROUND(25E-1) | ++------------+--------------+ +| 3 | 2 | ++------------+--------------+ +1 row in set (0.00 sec) +``` + +In MySQL, the result for approximate-value numbers depends on the C library, so `ROUND()` might not follow this rule on every platform. For inserts into a DECIMAL or integer column, the rounding uses [round half away from zero](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero).