# 有符号零

**有符号零**是浮点系统（尤其是 IEEE 754）的一种特性：零可以被表示两次，一次带正号（+0），一次带负号（−0）。在普通算术比较下，这两个值相等，但少数运算——例如除以零和 copysign 函数——能够区分它们。零的符号位记录了某个值来自零的哪一侧，这对于下溢、带分支割线的复函数，以及除法所产生的无穷大的方向都有意义。<sup>[1](https://www.ieee-754.net/background)</sup><sup> • </sup><sup>[2](https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF)</sup>

| 关键事实 | 详情 |
|---|---|
| 表示方式 | 在 IEEE 754 二进制格式中，零的指数和有效数全为零；符号位可以是 0 或 1，从而给出 +0 和 −0。<sup>[1](https://www.ieee-754.net/background)</sup> |
| 比较 | +0 与 −0 比较相等：在 C、Java 以及符合 IEEE 754 的语言中，`0.0 == -0.0` 为真。<sup>[3](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Signed-Zeros.html)</sup><sup> • </sup><sup>[4](https://cr.openjdk.org/~darcy/Ieee754TerminologyUpdate/2020-04-21/specs/float-terminology-jls.html)</sup> |
| 区分运算 | `1.0/0.0` 为 +∞，而 `1.0/-0.0` 为 −∞；除此之外，在算术上能区分这两个零的只有除以零或 CopySign 函数。<sup>[4](https://cr.openjdk.org/~darcy/Ieee754TerminologyUpdate/2020-04-21/specs/float-terminology-jls.html)</sup><sup> • </sup><sup>[2](https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF)</sup> |
| 符号传播 | 符号位按照由连续性导出的规则在算术运算中传播：(−3)(+0) = −0，(−0)/(−5) = +0，(−0) − (+0) = −0。<sup>[5](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)</sup> |
| 舍入规则 | 当符号相反的两个操作数的精确和或差为零时，除 roundTowardNegative 模式外，所有舍入模式下结果的符号均为 +0；在 roundTowardNegative 模式下为 −0。<sup>[6](https://harvest.usask.ca/bitstreams/19c1a056-d199-4ae0-81bc-465c0812595d/download)</sup> |
| 主要应用 | 有符号零使 log、sqrt、arccosh 等复初等函数能够在其分支割线上返回正确的值。<sup>[5](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)</sup><sup> • </sup><sup>[7](https://arxiv.org/abs/1712.10230)</sup> |
| 编译器控制 | GCC 的默认选项是 `-fsigned-zeros`；`-fno-signed-zeros` 允许进行忽略零的符号的优化，例如化简 `x+0.0` 或 `0.0*x`。<sup>[8](https://gcc.sourceware.org/onlinedocs/gcc/Optimize-Options.html)</sup> |

## 编码中的起源

IEEE 754 将浮点数存储为符号位、偏置指数和有效数。该标准将全零和全一的指数模式保留给特殊值。当指数和有效数均为全零时，值为零；由于符号位独立存储，它可以是 0 或 1——这正是负零能够与普通零并存的原因。<sup>[1](https://www.ieee-754.net/background)</sup>

同样的歧义也出现在整数记数法中。由于可表示的编码模式数量为偶数，零总会带来问题：二进制补码通过让负值比正值多一个来解决它，而二进制反码、符号-数值表示法和偏置记数法则允许存在负零。浮点数遵循符号-数值风格，因此继承了两种零。<sup>[1](https://www.ieee-754.net/background)</sup>

## 算术行为

卡汉（Kahan）关于该设计的论述将零的符号位描述为一个辅助变量，携带关于某个值为零的变量的一位信息——或误导信息；通常这一信息无关紧要，因为对于 x = +0 和 x = −0，`3 + x` 的结果相同。<sup>[5](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)</sup>

少数运算确实依赖于符号。除法最为明显：1/(+0) 为 +∞，1/(−0) 为 −∞。<sup>[5](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)</sup> Java 规范也陈述了同样的对比，即 `1.0/0.0` 与 `1.0/-0.0`，同时指出正零和负零比较相等，因此 `0.0 == -0.0` 为真，而 `0.0 > -0.0` 为假。<sup>[4](https://cr.openjdk.org/~darcy/Ieee754TerminologyUpdate/2020-04-21/specs/float-terminology-jls.html)</sup> 卡汉总结道，这两个零在算术上只能通过除以零（产生带相应符号的无穷大）或通过 IEEE 754/854 推荐的 CopySign 函数来区分。<sup>[2](https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF)</sup>

**传播规则。** 为了使符号位保持有用，它必须按照由连续性考虑导出的规则在算术运算中存活下来：(−3)(+0) = −0，(−0)/(−5) = +0，(−0) − (+0) = −0。有一条规则不得不任意选定：对于每个有限实数 s，s − s = +0，因此当两个相等但符号不同的值相减时，两种顺序都产生 +0，而非相反的符号。<sup>[5](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)</sup> 对于两个零操作数的加法和减法，精确零结果的符号为正，但在 roundTowardNegative 模式下为负。<sup>[6](https://harvest.usask.ca/bitstreams/19c1a056-d199-4ae0-81bc-465c0812595d/download)</sup>

## 为何保留零的符号

零的符号记录了一个太小而无法表示、但已知来自负轴或正轴的值的产生。<sup>[3](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Signed-Zeros.html)</sup>

**分支割线。** 最具影响的用途在复算术中。平方根和对数等函数在分支割线（复平面上主值发生跳跃的线）上不连续。例如，若排除负实轴，平方根是连续的；问题在于对负实数（形如 −x + i0，其中 x > 0）应返回什么。有符号零解决了这个问题：形如 x + i(+0) 的数位于割线的一侧，x + i(−0) 位于另一侧，函数返回相应的单侧极限。<sup>[9](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)</sup>

威廉·卡汉（William Kahan）是加州大学伯克利分校（University of California, Berkeley）电气工程与计算机科学荣休教授，也是 IEEE 754 的主要设计者之一，他在 1987 年的论文《复初等函数的分支割线，或无事生非的零的符号位》（*Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit*）中提出了这一处理方案。<sup>[5](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)</sup><sup> • </sup><sup>[10](https://people.eecs.berkeley.edu/~wkahan/index.htm)</sup> 他证明，在零具有可用符号的符合 IEEE 的机器上，复初等函数可以在趋近其边界割缝时乃至割缝上保持连续；而若零无符号，函数必须在每条割缝的一侧不连续，且诸如 ln(1/z) = −ln(z) 这样的常见恒等式在负实轴上失效。<sup>[5](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)</sup>

这一取舍是实实在在的。有符号零破坏了“x = y 蕴含 1/x = 1/y”这一关系：当 x = +0、y = −0 时该关系为假，因为倒数分别为 +∞ 和 −∞。IEEE 委员会认定，使用零的符号所带来的好处超过了其弊端。<sup>[9](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)</sup>

## 语言与编译器支持

C 标准的附录 F 和附录 G 以 IEEE 754 为基础，要求有符号浮点零；C 标准对复函数分支割线的处理也引用了有符号零。<sup>[11](https://www9.open-std.org/JTC1/SC22/WG14/issues/c11c17/issue0432.html)</sup> 在支持有符号零的平台上，Python 的 `math.copysign(1.0, -0.0)` 返回 −1.0，使符号在那里可被观察到。<sup>[12](https://docs.python.org/3/library/math.html?highlight=comb)</sup>

JavaScript 在 `==` 和 `===` 下将两个零视为相等，但在 `Object.is()` 下区分它们：`Object.is(0, -0)` 返回 false，而 `Object.is(-0, -0)` 返回 true。<sup>[13](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)</sup> 一元取负可以将 −0 引入表达式，而 `Math.atan2`、`Math.pow` 和 `Math.min` 等方法可以产生或保留它；例如 `Math.pow(-Infinity, -3)` 求值为 −0，`Math.min(-0, +0)` 求值为 −0。<sup>[14](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness)</sup>

编译器可以选择不遵守有符号零的规则。GCC 的 `-fno-signed-zeros` 允许进行忽略零的符号性的优化；由于 IEEE 算术规定了不同的 +0.0 和 −0.0，默认设置禁止化简 `x+0.0` 或 `0.0*x` 这类表达式，即使启用了 `-ffinite-math-only` 也是如此。默认选项为 `-fsigned-zeros`。<sup>[8](https://gcc.sourceware.org/onlinedocs/gcc/Optimize-Options.html)</sup>

## 实现质量

有符号零的实用性取决于整个软件栈——从硬件到编译器和数学库——的正确实现。布里斯托大学（University of Bristol）的安东·什捷连利赫特（Anton Shterenlikht）在 2019 年的一项基准研究中，测试了 Fortran 2008 复数内置函数 LOG、SQRT、ASIN、ACOS、ATAN、ASINH、ACOSH 和 ATANH 在分支割线上的表现，使用了全部三种 IEEE 二进制格式（binary32、binary64、binary128）的参数，覆盖八种编译器：GCC、Flang、Cray、Oracle、PGI、Intel、NAG 和 IBM。测试中出现了多处失败，包括结果符号错误以及意外的溢出、下溢或 NaN；该研究得出结论：这些实现的质量尚不足以消除在分支割线周围使用特殊代码的需要。<sup>[7](https://arxiv.org/abs/1712.10230)</sup>

现行标准 IEEE 754-2019 于 2019 年 6 月获批，2019 年 7 月作为 754-2008 的次要修订版发布，它继续规定有符号零，并相对于 754-2008 改变了推荐的 min/max 运算对 NaN 和有符号零的处理方式。<sup>[15](https://standards.ieee.org/ieee/754/6210/)</sup><sup> • </sup><sup>[16](https://grouper.ieee.org/groups/msc/ANSI_IEEE-Std-754-2019/background/)</sup>

## References

1. [Floating Point Background, IEEE-754.org](https://www.ieee-754.net/background)
2. [W. Kahan, Lecture Notes on the Status of IEEE Standard 754 for Binary Floating-Point Arithmetic (1997)](https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF)
3. [Signed Zeros, GNU C Language Manual](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Signed-Zeros.html)
4. [Update to IEEE 754-2019 Terminology, Java Language Specification notes](https://cr.openjdk.org/~darcy/Ieee754TerminologyUpdate/2020-04-21/specs/float-terminology-jls.html)
5. [W. Kahan, Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit (1987)](https://www.arithmazium.org/classroom/lib/Kahan_Branch_Cuts_for_Elementary_Functions.pdf)
6. [Table 2.2: Results when both addition/subtraction operands are zeros, University of Saskatchewan](https://harvest.usask.ca/bitstreams/19c1a056-d199-4ae0-81bc-465c0812595d/download)
7. [A. Shterenlikht, On quality of implementation of Fortran 2008 complex intrinsic functions on branch cuts, arXiv:1712.10230](https://arxiv.org/abs/1712.10230)
8. [Optimize Options, Using the GNU Compiler Collection (GCC)](https://gcc.sourceware.org/onlinedocs/gcc/Optimize-Options.html)
9. [D. Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic, Oracle docs](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)
10. [Prof. W. Kahan's web pages, UC Berkeley](https://people.eecs.berkeley.edu/~wkahan/index.htm)
11. [C issue 0432: Is 0.0 required to be a representable value?, WG14](https://www9.open-std.org/JTC1/SC22/WG14/issues/c11c17/issue0432.html)
12. [math, Mathematical functions, Python 3 documentation](https://docs.python.org/3/library/math.html?highlight=comb)
13. [Object.is(), MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
14. [Equality comparisons and sameness, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness)
15. [IEEE SA, IEEE 754-2019 Standard for Floating-Point Arithmetic](https://standards.ieee.org/ieee/754/6210/)
16. [ANSI/IEEE Std 754-2019 background, IEEE Working Group](https://grouper.ieee.org/groups/msc/ANSI_IEEE-Std-754-2019/background/)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic › Floating-point and mixed-precision arithmetic

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/signed-zero

English original: https://www.edgepedia.org/signed-zero
