[LCP]Rounding questions

Chuck Martin nrocinu at myrealbox.com
Thu Sep 5 17:46:08 UTC 2002


I'm having problems getting the round() function to work.  For some
reason, it always returns 0.  If I put the following line in a program,
it outputs "0" followed by a newline:

	printf("%f\n", round(390));

If I change "round" to "rint", it outputs 390, as it should.  Changing
"390" to "390.0" or "(double) 390" makes no difference, as I would expect.
I'm doing this in an existing program that already includes math.h, but
I've also had the problem that if I try to test this with a standalone
program, like this:

	#include <stdio.h>
	#include <math.h>

	int main()
	{
		printf("%f\n", round(390));
	}

it won't compile.  Instead, I get an error message that says "undefined
reference to `round'".  Why is it doing this?

Another concern I have is how portable round() is.  I realize this is a
Linux C programming list, but I'd like this to be portable.  The man page
for round(3) says that it conforms to C99, but I'm not sure how portable
that is, especially on older systems.  This program originally had it's
own rounding code, but I found a strange bug in it that I've been unable
to figure out.  If I have to resort to going back to the old code, I may
have to present that bug here to see if anyone else can figure it out,
but using round() would seem to be a simpler solution if I can make it
work and it's portable.

Thanks in advance.

Chuck





More information about the linuxCprogramming mailing list