当操作数为%
或(un)signed
时,C ++的模long
运算符会出现奇怪的行为。
为什么mod(signed int, unsigned int)
和mod(signed long long int, unsigned long long int)
会产生不同的结果?如果我想要正确一个(这里:11183),我该怎么办?
注意:如果我遵循算法,我应该致电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:有关我的系统的更多信息:
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
答案 0 :(得分:1)
尝试
UINT_MAX-3365+1
它不会打印" 3365",但会打印无符号整数的最大值减去3365 + 1,即4294963931
,即(unsigned int)(-3365) % (unsigned int)(15156)
。< / p>
所以unsigned int z = 4294963931 % 15156; printf("%u\n", z)
与11183
相同,并提供{{1}}。
答案 1 :(得分:0)
{
"code": 21215,
"message": "Account not authorized to call +MY_NUMBER. Perhaps you
need to enable some international permissions:
https://www.twilio.com/user/account/settings/international",
"more_info": "https://www.twilio.com/docs/errors/21215"
}
不是 modulo 。
另请参阅What's the difference between “mod” and “remainder”?,这里也适用C答案。
在C ++中,%
是分区的余数。 %
- &gt;该部门的剩余部分是-3365。
-3365/15156
首先出现以下错误,但由于转换为 unsigned 数学,这是正确的
(int)(-3365) % (int)(15156) = -3365