长整数出现意外的模数行为 [英] Unexpected modulo behavior with long integers

查看:67
本文介绍了长整数出现意外的模数行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当操作数是(un)signed long 时,C ++的模数运算符的行为会很奇怪.

The modulo % operator of C++ behaves strangely when operands are (un)signed or long.

为什么 mod(signed int,unsigned int) mod(signed long long int,unsigned long long int)会产生不同的结果?如果我想要一个正确的(在这里:11183)怎么办?

Why would mod(signed int, unsigned int) and mod(signed long long int, unsigned long long int) produce different results? How do I do if I want the correct one (here: 11183)?

注意::如果我遵循自己的算法,则应调用 uint64_t mod(int64_t,uint64_t).

Note: If I follow my algorithm, I should call uint64_t mod(int64_t, uint64_t).

// Tip: 11183 is the "correct" expected result.

(                   int)(-3365) % (                   int)(15156) = -3365
(  signed           int)(-3365) % (  signed           int)(15156) = -3365
(unsigned           int)(-3365) % (unsigned           int)(15156) = 11183
(  signed           int)(-3365) % (unsigned           int)(15156) = 11183
(unsigned           int)(-3365) % (  signed           int)(15156) = 11183

(              long int)(-3365) % (              long int)(15156) = -3365
(  signed      long int)(-3365) % (  signed      long int)(15156) = -3365
(unsigned      long int)(-3365) % (unsigned      long int)(15156) = 2555
(  signed      long int)(-3365) % (unsigned      long int)(15156) = 2555
(unsigned      long int)(-3365) % (  signed      long int)(15156) = 2555

(         long long int)(-3365) % (         long long int)(15156) = -3365
(  signed long long int)(-3365) % (  signed long long int)(15156) = -3365
(unsigned long long int)(-3365) % (unsigned long long int)(15156) = 2555
(  signed long long int)(-3365) % (unsigned long long int)(15156) = 2555
(unsigned long long int)(-3365) % (  signed long long int)(15156) = 2555

(          int_fast16_t)(-3365) % (          int_fast16_t)(15156) = -3365
(         uint_fast16_t)(-3365) % (          int_fast16_t)(15156) = 2555
(          int_fast16_t)(-3365) % (         uint_fast16_t)(15156) = 2555
(         uint_fast16_t)(-3365) % (         uint_fast16_t)(15156) = 2555

(          int_fast32_t)(-3365) % (          int_fast32_t)(15156) = -3365
(         uint_fast32_t)(-3365) % (          int_fast32_t)(15156) = 2555
(          int_fast32_t)(-3365) % (         uint_fast32_t)(15156) = 2555
(         uint_fast32_t)(-3365) % (         uint_fast32_t)(15156) = 2555

(          int_fast64_t)(-3365) % (          int_fast64_t)(15156) = -3365
(         uint_fast64_t)(-3365) % (          int_fast64_t)(15156) = 2555
(          int_fast64_t)(-3365) % (         uint_fast64_t)(15156) = 2555
(         uint_fast64_t)(-3365) % (         uint_fast64_t)(15156) = 2555

PS:有关我的系统的更多信息:

PS: More information about my system:

Linux pc-gi-446 4.4.0-36-generic #55-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux
g ++(Ubuntu 5.4.0-6ubuntu1〜16.04.4)5.4.0 20160609

Linux pc-gi-446 4.4.0-36-generic #55-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

推荐答案

不是取模.

另请参见"mod"和"remainder"有什么区别?,C答案也适用于此.

See also What's the difference between "mod" and "remainder"?, a C answer that applies here too.

在C ++中,是除法的余数. -3365/15156 ->0.除法的其余部分为-3365.

In C++, % is the remainder of a division. -3365/15156 --> 0. The remainder of the division is -3365.

(int)(-3365) % (int)(15156) = -3365  


以下内容一开始似乎是错误的,但是由于转换为 unsigned 数学,因此是正确的

(unsigned long int)(-3365) % (unsigned long int)(15156) = 2555
(ULONG_MAX + 1 -3365) % 15156 = 2555

这篇关于长整数出现意外的模数行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆