Re: Documentation of abs(3), div(3) etc.
Martijn van Buul <[email protected]>
| Newsgroups | gmane.os.netbsd.devel.general |
|---|---|
| Organization | Eindhoven University of Technology |
| Message-ID | <[email protected]> |
* Alan Barrett: > On Wed, 07 Feb 2007, Martijn van Buul wrote: >> * Christian Biere: >> > This is a lie anyway because the code looks like >> > >> > return a < 0 ? -a : a; >> > >> > whereas it obviously means >> > >> > return a < 0 ? -(unsigned)a : a; >> >> This is plain nonsense, on multiple grounds. First of all, you're casting >> a signed int (known to be negative) to an unsiged int, which is pretty >> broken to begin with, secondly, you're trying to negate the resulting >> unsigned number, which isn't any better. > > The suggested replacement code is correct. It is not. It is nonsensical, in that it is in effect the same as the supposedly "broken" code. > Unsigned arithmetic in C is defined in terms of modular arithmetic in > mathematics. My point was that the proposed change indicates is pointless, doesn't change a single opcode, and obviously indicates a lack of understanding. If you really think that signed int a; return (signed int) ( - (unsigned)a ); is in any better than signed int a; return -a; then I kindly suggest you catch up with how C works. > The original code would invoke undefined behaviour if it appeared in > user-written code. And what, pray tell, is the difference between this code appearing in user-written code and this code appearing in a userland library? It's just as broken, and just as undefined. It's just that the standard explicitly allows undefined behaviour, but there's no reason user-written code can't have the same definition. > (The mathematical result of -a might be outside the range representable by a > signed int, which gives undefined bahaviour.) Yes, you're repeating what I said before. A simple cast will *not* fix this. -- Martijn van Buul - [email protected]