boost::multiprecision::cpp_int:我想确认两个正cpp_int的除法截断为零
boost::multiprecision::cpp_int: I would like to confirm that division of two positive cpp_int's truncates towards zero
我用boost::multiprecision::cpp_int
,我找不到两个正的cpp_int
除向0
截断的证实;例如,
boost::multiprecision::cpp_int A {11};
boost::multiprecision::cpp_int B {4};
boost::multiprecision::cpp_int C = A / B; // 2, right?
在c++中,integer
类型中的A
和B
,标准要求对0
进行截断,使答案为C
= 2
。
我假设cpp_int
的工作方式相同- cpp_int
的答案也是2
。
然而,我找不到这个假设的证实。我也在boost::multiprecision::cpp_int
的源代码中寻找了几分钟,但我没有发现确认行为是微不足道的。
我想确认boost::multiprecision::cpp_int
在除两个正整数时按预期工作-即,它将结果截断到0
。
谢谢!
整数除法或模运算符的主要选项是向零舍入或向负无穷舍入。虽然在数学上趋向负无穷更正确,但C/c++趋向零。当被除数和除数有不同的符号时,这会影响除数或取模。向零舍入表示模数的结果与被除数的符号相同(或零),向负无穷舍入表示模数的结果与被除数的符号相同(或零)。
不涉及截断
截断假定存在一个中间的非整型结果。事实并非如此。唯一为cpp_int
(或任何其他多精度整数)定义的除法操作包括整数除法:
-
divide_qr
-设置q = x / y
和r = x % y
。template <class Backend, expression_template_option ExpressionTemplates> void divide_qr(const number-or-expression-template-type& x, const number-or-expression-template-type& y, number<Backend, ExpressionTemplates>& q, number<Backend, ExpressionTemplates>& r);
-
integer_modulus
-返回x % val;
template <class Integer> Integer integer_modulus(const number-or-expression-template-type& x, Integer val);
除了整数除法这个定义良好的概念,并且出现在所有主流编程语言中,否则就没有任何意义,因为(至少对于正整数*¹*)
x == (q*y) + r
应为真
¹ IIRC混合符号模在c++中未定义;除非你能在代码/文档
相关文章:
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 从 'int' 到 'int*' CPP 的转换无效
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 定义.cpp中常数int/char*
- 需要获取 void 或 int 的 Cpp 模板函子
- Cpp 未定义引用 'Apache:: thrift:: transport:: TSocket:: TSocket (std:: string const&, int)'
- 这个变量如何在 cpp 中用作 int 和数组?
- 如何在 CPP 中使用 int 迭代器打印数组/列表的向量?
- Visual C 计算器类让我在多个CPP文件上传播最低的INT
- 从函数 CPP 返回 int 或 string.c_str() 时的两个不同的行为
- 希望修复 main.cpp 与"std::istream&<< int&"运算符不匹配
- 核心 cpp 指南中 f(T*, int) 接口与 f(span<T>) 接口的含义
- 数据类型在 CPP 中将 int 转换为双倍
- 如何将强调字符转换为数字(INT)CPP
- CPP 将 int 分配给字符串恶作剧
- 在cpp中串联字符串和int作为文件名
- 主.cpp:63:12:错误:从 'int (*)[4]' 到 'int' [-允许] 的转换无效。我做错了什么?
- 当我在c/cpp中执行int*p=p时会发生什么
- 语句问题中要字符串的CPP int