ckd_sub
来自 zh.cppreference.net
| 定义于头文件 <stdckdint.h>
|
||
#define ckd_sub( result, a, b ) /* implementation-defined */
// exposed interface:
bool ckd_sub( type1* result, type2 a, type3 b );
|
(自C23起) | |
计算减法 x - y 并将结果存储到 *result 中。减法执行时就好像两个操作数都在具有无限范围的带符号整数类型中表示,然后结果从该整数类型转换为 type1。如果赋给 *result 的值正确地表示了运算的数学结果,则返回 false。否则,返回 true。在这种情况下,赋给 *result 的值是运算的数学结果回绕到 *result 宽度的值。
参数
| a, b | - | 整数值 |
| result | - | 存储结果的位置的地址 |
返回值
false 如果赋给 *result 的值正确地表示了减法的数学结果,则 true 否则。
注意
type2 和 type3 都应为除“普通” char、bool、位精确整数类型或枚举类型之外的任何整数类型,并且它们可以相同。*result 应为除“普通” char、bool、位精确整数类型或枚举类型之外的任何整数类型的可修改左值。
建议在 type2 或 type3 不是合适的整数类型时,或者 *result 不是合适整数类型的可修改左值时,产生诊断消息。
示例
运行此代码
#include <limits.h>
#include <stdckdint.h>
#include <stdint.h>
#include <stdio.h>
int main()
{
int16_t result1;
constexpr int8_t x1 = -2;
constexpr int16_t y1 = 1;
const bool underflow1 = ckd_sub(&result1, x1, y1);
printf("%i - %i == %i (%s)\n", x1, y1, result1, underflow1 ? "Underflow" : "OK");
int16_t result2;
constexpr int8_t x2 = -2;
constexpr int16_t y2 = INT16_MAX;
const bool underflow2 = ckd_sub(&result2, x2, y2);
printf("%i - %i == %i (%s)\n", x2, y2, result2, underflow2 ? "Underflow" : "OK");
int32_t result3;
constexpr int8_t x3 = -2;
constexpr int16_t y3 = INT16_MAX;
const bool underflow3 = ckd_sub(&result3, x3, y3);
printf("%i - %i == %i (%s)\n", x3, y3, result3, underflow3 ? "Underflow" : "OK");
}
输出:
-2 - 1 == -3 (OK)
-2 - 32767 == 32767 (Underflow)
-2 - 32767 == -32769 (OK)
参考文献
- C23标准(ISO/IEC 9899:2024):
- 7.20.1 ckd_受检整数运算宏(第311页)
参阅
(C23) |
两个整数的受检加法运算 (泛型函数宏) |
(C23) |
两个整数的受检乘法运算 (泛型函数宏) |
C++ 文档 关于 ckd_sub
| |