ckd_mul
|
定義済みヘッダー
<stdckdint.h>
|
||
|
template
<
class
type1,
class
type2,
class
type3
>
bool ckd_mul ( type1 * result, type2 a, type3 b ) ; |
(C++26以降) | |
x × y の乗算を計算し、結果を * result に格納します。乗算は、両オペランドが無限の範囲を持つ符号付き整数型で表現されているものとして実行され、結果がこの整数型から type1 に変換されます。 * result に代入される値が演算の数学的な結果を正しく表す場合、 false を返します。それ以外の場合、 true を返します。この場合、 * result に代入される値は、演算の数学的な結果が * result の幅にラップアラウンドされた値です。
目次パラメータ
戻り値false 乗算の数学的結果が * result に正しく代入された場合は、 true それ以外の場合。 注記
関数テンプレート
type1 、 type2 、および type3 の各型は、cv修飾されていない符号付きまたは符号なし整数型です。 type2 または type3 が適切な整数型でない場合、あるいは * result が適切な整数型の変更可能な左辺値でない場合、診断メッセージを生成することが推奨されます。 例このコードを実行 #include <climits>
#include <cstdint>
#include <print>
#include <stdckdint.h>
int main()
{
uint8_t x;
uint16_t y;
uint16_t result_uint16;
uint32_t result_uint32;
bool overflow;
x = 2;
y = 21;
overflow = ckd_mul(&result_uint16, x, y);
std::println("{} * {} == {} ({})", x, y, result_uint16, overflow ? "Overflow" : "OK");
x = 2;
y = UINT16_MAX;
overflow = ckd_mul(&result_uint16, x, y);
std::println("{} * {} == {} ({})", x, y, result_uint16, overflow ? "Overflow" : "OK");
x = 2;
y = UINT16_MAX;
overflow = ckd_mul(&result_uint32, x, y);
std::println("{} * {} == {} ({})", x, y, result_uint32, overflow ? "Overflow" : "OK");
}
出力: 2 * 21 == 42 (OK)
2 * 65535 == 65534 (Overflow)
2 * 65535 == 131070 (OK)
参考文献
関連項目
| ||||||||||||