简体   繁体   English

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

I am using boost::multiprecision::cpp_int , and I cannot find confirmation that division of two positive cpp_int 's truncates towards 0 ; 我正在使用boost::multiprecision::cpp_int ,但找不到两个正数cpp_int的除法向0截断的确认; ie, that 即,

boost::multiprecision::cpp_int A {11};
boost::multiprecision::cpp_int B {4};

boost::multiprecision::cpp_int C = A / B; // 2, right?

In C++, were A and B builtin integer types, the standard requires truncation towards 0 , so that the answer would be C equals 2 . 在C ++中, AB内置integer类型,则标准要求将其截断为0 ,因此答案将为C等于2

I assume that cpp_int works the same way - that the answer is 2 for cpp_int , also. 我认为cpp_int的工作方式相同-答案是2cpp_int ,也。

However, I cannot find confirmation of this assumption. 但是,我找不到这种假设的证实。 I have also looked for a few minutes in the source code for boost::multiprecision::cpp_int , but I did not find it trivial to confirm the behavior. 我还在boost::multiprecision::cpp_int的源代码中boost::multiprecision::cpp_int ,但我发现确认该行为并不容易。

I would like to confirm that boost::multiprecision::cpp_int works as expected when dividing two positive integers - namely, that it truncates the result towards 0 . 我想确认boost::multiprecision::cpp_int在将两个正整数相除时能按预期工作-即,它将结果截断为0

Thanks! 谢谢!

There is no truncation involved. 没有截断。

Truncation assumes that there is an intermediate, non-integral, result. 截断假定存在中间的非整体结果。 This is not the case. 不是这种情况。 The only division operations defined for cpp_int (or any other multiprecision integer) involve integer division : cpp_int (或任何其他多精度整数)定义的唯一除法运算涉及整数除法

  • divide_qr - Sets q = x / y and r = x % y . divide_qr设置q = x / yr = 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 - Returns x % val; integer_modulus返回x % val;

     template <class Integer> Integer integer_modulus(const number-or-expression-template-type& x, Integer val); 

Apart from integer division being this well-defined concept, and appearing throughout all mainstream programming languages, it wouldn't make any sense to have it otherwise, because (at least for positive integers* ¹ *) 除了整数除法是一个定义明确的概念,并且出现在所有主流编程语言中,否则除此以外就没有任何意义,因为(至少对于正整数* ¹ *)

x == (q*y) + r

should be true 应该是真的


¹ IIRC mixed-sign modulo is undefined for C++; ¹对于C ++,未定义IIRC混合符号模; I wouldn't expect guarantees here unless you can find them in the code/documentation 除非您可以在代码/文档中找到保证,否则我不会期望这里有保证

The main options for integer division or modulo operators is rounding towards zero or rounding towards negative infinity. 整数除法或模运算符的主要选择是四舍五入为零或四舍五入为负无穷大。 Although rounding towards negative infinity is mathematically more correct, C / C++ round towards zero. 虽然在数学上朝负无穷大舍入更正确,但C / C ++朝零趋近。 This affects division or modulo when the dividend and divisor have differing signs. 当被除数和除数具有不同的符号时,这会影响除法或取模。 Rounding towards zero means that the result of modulo has the same sign as the dividend (or zero), and rounding towards negative infinity means the result of modulo has the same sign as the divisor (or zero). 向零舍入意味着模的结果与被除数(或零)具有相同的符号,向负无穷大舍入意味着模数的结果与除数(或零)具有相同的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM