bkmgit edited a comment on pull request #11720: URL: https://github.com/apache/arrow/pull/11720#issuecomment-971519070
@pitrou An example following https://stackoverflow.com/questions/1855459/maximum-value-of-int is below: ```c++ #include<iostream> #include<limits> int main() { int imin = std::numeric_limits<int>::min(); int imax = std::numeric_limits<int>::max(); int ia = 105; int ib = -2; int c; // Typically range of unsigned int is [0,4294967295] unsigned int d; // Typically range of int is [-2147483648,2147483647] int ba = imax-1; int bb = -2; int bc = 2; int16_t i16a = std::numeric_limits<int16_t>::max() -1; int i16a_32 = std::numeric_limits<int16_t>::max() -1; int16_t i16b = -2; int16_t i16c = 2; int16_t i16d; uint16_t i16e; // Do some multiplication c = ia * ib; std::cout << c << std::endl; // answer should be -210 // cast to unsigned and do multiplication c = (int) (((unsigned int) ia ) * ((unsigned int) ib )); std::cout << c << std::endl; // answer should be -210 after casting again // cast to unsigned and do multiplication c = (int) (((unsigned int) ba) * ((unsigned int) bb )); std::cout << c << std::endl; // Cannot represent answer -4294967292 get overflow and output 4 // cast to unsigned and do multiplication d = (unsigned int) (((unsigned int) ba) * ((unsigned int) bb )); std::cout << d << std::endl; // output should be -4294967292 but cannot represent it so get output 4 // cast to unsigned and do multiplication c = (int) (((unsigned int) ba) * ((unsigned int) bc )); std::cout << c << std::endl; // output should be 4294967292 which cannot be represented and get -4 // cast to unsigned and do multiplication d = (unsigned int) (((unsigned int) ba) * ((unsigned int) bc )); std::cout << d << std::endl; // output should be 4294967292 // cast to unsigned and do multiplication c = (int) (((unsigned int) ba) * ((unsigned int) bc )); std::cout << c << std::endl; // output should be 4294967292 which cannot be represented and get -4 // cast to int and do multiplication c = (int) ((( int) i16a) * (( int) i16b )); std::cout << c << std::endl; // output should be -65532 // cast to unsigned int and do multiplication i16c = (int16_t) (((unsigned int) i16a ) * ((unsigned int) i16b )); std::cout << i16c << std::endl; // output should be -65532 which cannot be represented and get 4 // cast to unsigned int and do multiplication i16d = (int16_t) (((unsigned int) i16a ) * ((unsigned int) i16c )); std::cout << i16d << std::endl; // output should be 65532 which cannot be represented and get -8 // cast to unsigned int and do multiplication i16d = (uint16_t) (((unsigned int) i16a ) * ((unsigned int) i16b )); std::cout << i16d << std::endl; // output should be -65532 which cannot be represented and get 4 // do multiplication i16e = i16a * i16b; std::cout << i16e << std::endl; // output should be -65532 which cannot be represented and get 4 return 0; } ``` The ranges for the types do depend on the machine being used and also the programming language, but for C++, a minimum range is guaranteed by the standard. Can turn these into tests and put them in -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
