Hi, your code is really concise. However, I don't understand this line :
if (x < 0 && -x < 0) return 0;
what do you mean by "-x overflows", the input value is negative and overflow?
Could you give me some explain in detail? Thank you.
If you use the following condition judged the overflow:
(INTMAX - X%10-10*res) < 0,
INTMAX - X%10 < 10*res
When it acctually overflows, 10res is negative, INTMAX - X%10 is positive, the condtion fails, it can not contains the overflow situation. The sign of (INTMAX - X%10-10res) may be positive(can do some experiment), the condition fails, can not contains the overflow.
But use (INTMAX-X%10)/10 < res, it is coducted from INTMAX < 10*res + X%10. transfrom to (INTMAX-X%10)/10 < res, make the both side of the less equation are all positive, the same range. It can satify the purpose. And the value of res belongs to the corrent range.