6 ms·
AI rewrote to avoid undefined behavior: int average(int x, int y) { long sum = (long)x + y; if(sum > INT_MAX || sum < INT_MIN) return -1; //
by oneshtein 2y ago
AI rewrote to avoid undefined behavior:
int average(int x, int y) {
long sum = (long)x + y;
if(sum > INT_MAX || sum < INT_MIN)
return -1; // or any value that indicates an error/overflow
return (int)(sum / 2);
}
- Jaxan 2y agoI’m not convinced that solution is much better. It can be improved to x/2 + y/2 (which still gives the wrong answer if both inputs are odd).
- josefx 2y ago> long sum = (long)x + y; There is no guarantee that sizeof(long) > sizeof(int), in fact the GNU libc documentation states that int and long have the same size on the majority of supported platforms. https://www.gnu.org/software/libc/manual/html_node/Range-of-Type.html https://www.gnu.org/software/libc/manual/html_node/Range-of-... > return -1; // or any value that indicates an error/overflow -1 is a perfectly valid average for various inputs. You could return the larger type to encode an error value that is not a valid output or just output the error and average in two distinct variables. AI and C seem like a match made in hell.
- cesarb 2y ago> There is no guarantee that sizeof(long) > sizeof(int), in fact the GNU libc documentation states that int and long have the same size on the majority of supported platforms. That used to be the case for 32-bit platforms, but most 64-bit platforms in which GNU libc runs use the LP64 model, which has 32-bit int and 64-bit long. That documentation seems to be a bit outdated. (One notable 64-bit platform which uses 32-bit for both int and long is Microsoft Windows, but that's not one of the target platforms for GNU libc.)
- throwaway2037 2y agoI don't know why this answer was downvoted. It adds valuable information to this discussion. Yes, I know that someone already pointed out that sizeof(int) is not guaranteed on all platforms to be smaller than sizeof(long). Meh. Just change the type to long long, and it works well.
- School-Cotton 2y agoI always downvote all AI-generated content regardless of whether it’s right or wrong, because I would like to discourage people from posting it.
- gf000 2y agoIt literally returns a valid output value as an error.
- oneshtein 2y agoAn error value is valid output in both cases.
- MaxBarraclough 2y agoThe code is unarguably wrong. average(INT_MAX,INTMAX) should return INT_MAX, but it will get that wrong and return -1. average(0,-2) should not return a special error-code value, but this code will do just that, making -1 an ambiguous output value. Even its comment is wrong. We can see from the signature of the function that there can be no value that indicates an error, as every possible value of int may be a legitimate output value. It's possible to implement this function in a portable and standard way though, along the lines of [0]. [0] https://stackoverflow.com/a/61711253/ https://stackoverflow.com/a/61711253/ (Disclosure: this is my code.)
- MaxBarraclough 2y agoToo late for me to edit: as josefx pointed out, it also fails to properly address the undefined behavior. The sums INT_MAX + INT_MAX and INT_MIN + INT_MIN may still overflow despite being done using the long type. That won't occur on an 'LP64' platform, [0] but we should aim for proper portability and conformance to the C language standard. [0] https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_m...
- josefx 2y ago
- immibis 2y agoWe're about to see a huge uptick in bugs worldwide, aren't we?
- School-Cotton 2y agoPlease stop posting AI-generated content to HN. It’s clear the majority of users hate it, given that it gets swiftly downvoted every time it’s posted.