linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Why does math.h not declare function round?
@ 2007-09-24  4:10 Plato
  2007-09-24  7:10 ` Steve Graegert
  0 siblings, 1 reply; 2+ messages in thread
From: Plato @ 2007-09-24  4:10 UTC (permalink / raw)
  To: linux-c-programming

Hi!
    I made such a program foo.c:

#include <math.h>

int main()
{
    double bar=1.2;
    printf("%f\n", round(bar));
}
    
Then compile and run:
[root@so41 tmp]# gcc foo.c -ofoo -lm
[root@so41 tmp]# ./foo
0.000000

The result was obviously wrong. Then I add statement:
#include <math.h>
double round(double x);

int main()
{
    double bar=1.2;
    printf("%f\n", round(bar));
}

Then compile and run:
[root@so41 tmp]# gcc foo.c -ofoo -lm
[root@so41 tmp]# ./foo
1.000000

Now it's right.

I wonder why function round not declared in math.h. Anyone have idea?

Thx.




Plato
2007-09-24


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Why does math.h not declare function round?
  2007-09-24  4:10 Why does math.h not declare function round? Plato
@ 2007-09-24  7:10 ` Steve Graegert
  0 siblings, 0 replies; 2+ messages in thread
From: Steve Graegert @ 2007-09-24  7:10 UTC (permalink / raw)
  To: Plato; +Cc: linux-c-programming

On 9/24/07, Plato <zhangguoqiang@xunlei.com> wrote:
> Hi!
>     I made such a program foo.c:
>
> #include <math.h>
>
> int main()
> {
>     double bar=1.2;
>     printf("%f\n", round(bar));
> }
>
> Then compile and run:
> [root@so41 tmp]# gcc foo.c -ofoo -lm
> [root@so41 tmp]# ./foo
> 0.000000
>
> The result was obviously wrong. Then I add statement:
> #include <math.h>
> double round(double x);
>
> int main()
> {
>     double bar=1.2;
>     printf("%f\n", round(bar));
> }
>
> Then compile and run:
> [root@so41 tmp]# gcc foo.c -ofoo -lm
> [root@so41 tmp]# ./foo
> 1.000000
>
> Now it's right.
>
> I wonder why function round not declared in math.h. Anyone have idea?

It is available in C99 mode only (see <bits/mathcalls.h>):

#ifdef __USE_ISOC99
__MATHCALLX (round,, (_Mdouble_ __x), (__const__));

To write fully portable code use floor() and ceil() instead.

	\Steve

--

Steve Grägert <steve@graegert.com>
Internet: http://digitalether.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-09-24  7:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-09-24  4:10 Why does math.h not declare function round? Plato
2007-09-24  7:10 ` Steve Graegert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).