Implementing C99's roundf(), round(), and roundl()
David Schultz
das at FreeBSD.ORG
Sun Nov 30 13:42:23 PST 2003
On Sat, Nov 29, 2003, Steve Kargl wrote:
> On Sat, Nov 29, 2003 at 12:09:11AM -0800, David Schultz wrote:
> > On Fri, Nov 28, 2003, Steve Kargl wrote:
> > > Can the math functions round[fl]() be implemented in
> > > terms of other math(3) functions and still conform to the
> > > C99 and POSIX standard? For example,
> > >
> > > #include <math.h>
> > >
> > > float roundf(float x) {
> > > float t;
> > > if (x >= 0.0) {
> > > t = ceilf(x);
> > > if ((t - x) > 0.5) t -= 1.0;
> > > return t;
> > > } else {
> > > t = ceilf(-x);
> > > if ((t + x) > 0.5) t -= 1.0;
> > > return -t;
> > > }
> > > }
> >
> > This looks correct to me at first glance, modulo possible problems
> > with overflow. It's valuable to have simple MI implementations of
> > these functions to avoid hampering efforts to port FreeBSD to new
> > architectures. Faster MD versions can always be added later. (I
> > noticed the other day that Intel has actually released an
> > optimized IA64 libm, which we should consider importing.)
>
> I don't undrestand your overflow comment. ceil[f]() can return Inf
> and nan, but in those cases round[f]() should also return Inf and nan.
> The two operations, (t-x) and (t+x), should yield a value in the
> range [0,1). I'll submit a PR with a man page.
The concern was that ceil() could round a number up to infinity
when round() is supposed to round the number down. But now that I
think about it at a reasonable hour, this concern is clearly
bogus. In base two floating point representations, there isn't
enough precision to get numbers that large with nonzero fractional
parts.
> As a side comment, we need to start coding the missing C99 math(3)
> functions because GCC is moving to using these in their CVS
> development trees.
Really? Which ones? I don't think I'll have time to deal with
this until January, but then again, we're not doing another gcc
import before then.
More information about the freebsd-standards
mailing list