Hacker News new | past | comments | ask | show | jobs | submit login

But the only way to account for overflow is to avoid comparison by subtraction.



You can also do the comparison over larger integers, for example

  int cmp(int x, int y) {
    return (int) (((long)x-(long)y)>>32);
  }


In mainstream compilers, even on x64, sizeof(long) == sizeof(int). You need long long instead. Of course, in the general case there's exactly zero guarantee that there even exists an integral type wider than int.


The C99 language standard requires support for long long.


But an implementation where sizeof(int) == sizeof(long) == sizeof(long long) is allowed by the specification.


On a (slightly) more practical level: there exists some largest integer type. You may someday need to sort it. If you want your sort to always work, you can't afford to overflow.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: