Subject: Re: Documentation of abs(3), div(3) etc.
To: Alan Barrett <apb@cequrux.com>
From: Krister Walfridsson <cato@df.lth.se>
List: tech-misc
Date: 02/10/2007 16:24:03
On Fri, 9 Feb 2007, Alan Barrett wrote:

>>> the manpages for abs(3) and its variants define behavior for the
>>> "most negative integer" whereas the standard explicity states that
>>> the behavior is undefined if the result cannot be represented.
>
> I'd like out man page to both document what we do, and document what
> parts of that are non-standard.  At present, out abs(3) seems to
> accurately document what we do, but not that part of it is undefined by
> the standard.

But the current code does in fact invoke undefined behavior when
called with the value of the most negative integer.  :)

And gcc does recognize abs() and will generate code directly and optimize 
it using the definition as written in the standard.  I.e. the libc abs() 
will in general not be called, so users cannot rely on any "extensions" 
anyway.


I think it is better to update the man page to say the same thing as
the standard, instead of uglifying the implementation with casts
and crippling gcc in random ways...

    /Krister