约 1089 字

约 5 分钟

#C 语言标准库宏 samequantum

下面以函数形式说明宏的调用接口。

#include <tgmath.h>

bool samequantum(generic_value x, generic_value y);

说明

比较两个十进制浮点值的量子指数,可区分数值相等而末尾零不同的表示。 示例的实参类型选择 samequantumd64。

量子指数比较独立于两个数的大小。

该宏要求十进制浮点参数。按参与分派的参数类型选择相应的十进制精度。整数参与时按 _Decimal64 处理。

自 C23 起提供。需要实现定义 __STDC_IEC_60559_DFP__ 并提供对应的十进制浮点接口。

参数

  • x - 参与运算的实浮点值。
  • y - 第二个实浮点操作数。

返回值

  • 有相同量子指数时返回 true,否则返回 false。两者同为 NaN 或同为无穷时也返回 true。

#示例

使用 GCC 时,在链接命令末尾添加 -lm。

#include <tgmath.h>
#include <stdio.h>

int main(void)
{
    bool computed = samequantum(1.20dd, 2.50dd);
    printf("%d\n", (int)computed);
    return !(computed == 1);
}

运行结果。

1

#相关接口

#参考标准

更新: 2026/11/28

作者: planc

创建: 2026/11/28