3 ms·
> Correct code would be: ... Nah, it's still really broken :-) Here, let's take all the confusing stuff out - no arrays, sinc functions, etc.. This compiles
by xscott 5y ago
> Correct code would be: ...
Nah, it's still really broken :-)
Here, let's take all the confusing stuff out - no arrays, sinc functions, etc.. This compiles cleanly with -Wall and -Wextra
#include <stdio.h>
int main() {
size_t len = 6;
double scale = 0.1;
for(size_t ii=0; ii<len ;ii++) {
printf("%.2lf\n", -scale*((len+1)/2-ii));
}
}
Using size_t, it prints:
-0.30
-0.20
-0.10
-0.00
-1844674407370955264.00
-1844674407370955264.00
If you change it to use to `ssize_t`, you get the desired result:
-0.30
-0.20
-0.10
-0.00
0.10
0.20
> This reinforces my belief that unsigned integers [...]
I hope to show you that's wrong :-)
> they (occasionally) make off-by-one errors explode loudly
The problem isn't an off-by-one error, it's unsigned underflow.