whattheactualf3
whattheactualf3 on Hacker News
Re:Almost Always Unsigned
on Sunday January 02, 2022 @07:40AM (#29768255)
Here's an example:
unsigned int length_a = 10;
unsigned int length_b = 20;
if (length_a - length_b < 0)
{
printf("negative diff\n");
}
else
{
printf("positive diff\n");
}
I would argue that there is plenty of code like that in the wild. It looks innocent and harmless but it gives a completely surprising result.
Re:Almost Always Unsigned
on Sunday January 02, 2022 @07:24AM (#29768169)
It does not express intent. I remember being a junior C programmer and using "unsigned" to express my intent (it's greater than or equal to zero).
In reality, like all C keywords, it's completely misleading and actually really means "weird_arithmetic_mode_that_will_mess_you_up".
Re:Almost Always Unsigned
on Sunday January 02, 2022 @07:22AM (#29768155)
Is it called "unsigned" though?
In Racket (and in mathematics) we call those natural numbers. The very notion of "unsigned" was invented by video game programmers at Bell Labs. In mathematics positive numbers are still signed (they just have a positive sign).
Re:Almost Always Unsigned
on Sunday January 02, 2022 @07:18AM (#29768132)
The argument is really strange. It says that unsigned ints can't become negative and it's a good thing. But they do that by instead having crazy high values. How is that a win?
You could argue that it doesn't matter because an incorrect program is incorrect regardless. True. At least with signed integers you can represent differences between positive values. How do you do that with unsigned?