News for hackers. Stuff that matters.
Re:Zlib-rs is faster than C by throwaway2037 on Monday March 17, 2025 @04:29AM (#43386269)
I 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.
Re:Zlib-rs is faster than C by gf000 on Monday March 17, 2025 @04:33AM (#43386284)
It literally returns a valid output value as an error.
Re:Zlib-rs is faster than C by oneshtein on Monday March 17, 2025 @11:22AM (#43389527)
An error value is valid output in both cases.
Re:Zlib-rs is faster than C by MaxBarraclough on Monday March 17, 2025 @06:50PM (#43393545)
The 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/ (Disclosure: this is my code.)

Re:Zlib-rs is faster than C by MaxBarraclough on Tuesday March 18, 2025 @04:19AM (#43396843)
Too 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_m...

Re:Zlib-rs is faster than C by josefx on Monday March 17, 2025 @04:53AM (#43386391)
> Meh. Just change the type to long long, and it works well.

C libraries tend to support a lot of exotic platforms. zlib for example supports Unicos, where int, long int and long long int are all 64 bits large.

Re:Zlib-rs is faster than C by NobodyNada on Monday March 17, 2025 @11:09AM (#43389387)
Copypasting a comment into an LLM, and then copypasting its response back is not a useful contribution to a discussion, especially without even checking to be sure it got the answer right. If I wanted to know what an LLM had to say, I can go ask it myself; I'm on HN because I want to know what people have to say.
Re:Zlib-rs is faster than C by umanwizard on Tuesday March 18, 2025 @01:33AM (#43396082)
I always downvote all AI-generated content regardless of whether it’s right or wrong, because I would like to discourage people from posting it.