[LCP]Rounding questions

Greg Black gjb at gbch.net
Fri Sep 6 07:26:11 UTC 2002


Steve Baker wrote:

| Chuck Martin <nrocinu at myrealbox.com> wrote:
| > On Thu, Sep 05, 2002 at 12:36:39PM -0500, Steve Baker wrote:
| > >   It seems perplexing, but the problem is that round is not prototyped in
| > > math.h, or anywhere for that matter.  This is clearly a glibc maintainer
| > > issue.  Add to your program at the top:
| > > 
| > > double round(double);
| >
| > Thank you!  This works.  Do you happen to know why rint() works without
| > doing this?  I can't seem to find a prototype for it anywhere, including
| > math.h.
| 
|   Augh... I _always_ forget about features.h.  round() is defined after all,
| but only if you have _ISOC99_SOURCE defined at the very very top of your C
| file before any #includes (or _GNU_SOURCE, which turns on other things as
| well).  If you look in /usr/include/features.h you'll find all the "features"
| you can select from.

And only if your system has features.h and all the rest of
panoply.  None of the BSD systems I have access to here has
this.

| > Now this brings me to the other question I asked when I started this
| > thread.  How portable is this?  The man page says it conforms to C99,
| > but I'm not up on all of the standards and where they're all used.
| > Will using this instead of my own rounding routine cause problems in
| > portability, and if so, to what extent?
| 
|   Any system that conforms to C99 should be OK, and most should, so I imagine
| it's portable.  You may have to jump through a hoop or two like this to get it
| to work though.

Given that the functionality of round() is trivial to implement
with the C89 floor() and ceil() functions, it would be much
simpler to implement it in terms of those.  C99 is something
that wise people will avoid for several more years (possibly for
ever) if they don't want this sort of grief.

Most compilers are either not C99-compliant or are only partly
there (and probably with plenty of bugs).  Unless you have a
real need for something that is only available in C99 (which is
a remote probability in my experience) or unless you want to be
part of the push to bring C99 into mainstream use, it's better
to act as if it does not exist.

Greg




More information about the linuxCprogramming mailing list