我在我的Mac OS X的XCode 7.2.1尝试一些无符号长长的意外行为
value: 18446744073709551615
ULLONG_MAX: 18446744073709551615
但输出是 -
value: 0
ULLONG_MAX: 18446744073709551615
下面的变化也没有什么区别。
uint64 N = 100000ull;
uint64 value = (pow(N, 4ull) + 2ull * pow(N, 3ull) + 3ull * pow(N, 2ull) + 2ull * N)/4ull;
在我的终端产量运行gcc --version
命令 -
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
我试图上面Hackerrank克++编译器4.9.2码和输出是正确的。
发生了什么?是Clang还是我的操作系统(Mac OS X 10.11)?
如果您将'long double'分配给您,您会看到结果是_not_'ULLONG_MAX'。相反,它会溢出。 '长双倍值= pow(N,4.0L)+ 2.0L * pow(N,3.0L)+ 3.0L * pow(N,2.0L)+ 2.0L * N)/ 4.0L;' – paddy
谢谢:) 'long double'可以容纳更多的数据,这对于检查在这种情况下是否发生溢出确实有帮助。 –