Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

round function

473 views
Skip to first unread message

Titus Cheung

unread,
Sep 27, 2001, 4:33:35 AM9/27/01
to
Hello,

Is there a function in the math.h library called "round"? I just need to
round a double variable into an integer value, not an integer variable.

Thanks


John

unread,
Sep 27, 2001, 4:43:03 AM9/27/01
to

"Titus Cheung" <ti...@ieee.org> wrote in message
news:zvBs7.17919$ob.4...@news1.rdc1.bc.home.com...

> Hello,
>
> Is there a function in the math.h library called "round"? I just need to
> round a double variable into an integer value, not an integer variable.
>

float f = 1.345;
float f1 = (int) f;
printf("%g", f1);


John


Mal Kay

unread,
Sep 27, 2001, 6:36:15 AM9/27/01
to

From the draft n869 of the standard we have:
[#1] When a finite value of real floating type is converted |
to an integer type other than _Bool, the fractional part is
discarded (i.e., the value is truncated toward zero). If
the value of the integral part cannot be represented by the
integer type, the behavior is undefined.

Truncating toward zero is not what most would think of as rounding.
Also the undefined behaviour is a significant negative.
#include <math.h>
...
float f=1.345, f1;
f1 = floor(f+0.5);
This is close enough to satisfy most peoples rounding requirements.

Malcolm Kay

Russ Bobbitt

unread,
Sep 27, 2001, 6:36:12 AM9/27/01
to
On Thu, 27 Sep 2001 08:33:35 GMT, "Titus Cheung" <ti...@ieee.org>
wrote:

>Hello,
>
>Is there a function in the math.h library called "round"? I just need to
>round a double variable into an integer value, not an integer variable.

No, but there is ceil() for rounding up and floor() for rounding down.
If you're rounding down, you can also use a cast.

e.g.

double foo=5.5;
printf("%d\n",(int)foo);

Russ

Russ Bobbitt

unread,
Sep 27, 2001, 6:40:07 AM9/27/01
to

After reading Mal Kay's response and quote, you should probably stick
with floor() for rounding down :-)

Russ

John

unread,
Sep 27, 2001, 8:23:21 AM9/27/01
to

"Mal Kay" <malco...@adelaide.on.net> wrote in message

>
> From the draft n869 of the standard we have:
> [#1] When a finite value of real floating type is converted |
> to an integer type other than _Bool, the fractional part is
> discarded (i.e., the value is truncated toward zero). If
> the value of the integral part cannot be represented by the
> integer type, the behavior is undefined.
>
> Truncating toward zero is not what most would think of as rounding.
> Also the undefined behaviour is a significant negative.
> #include <math.h>
> ...
> float f=1.345, f1;
> f1 = floor(f+0.5);
> This is close enough to satisfy most peoples rounding requirements.

Or using

f1 = ceil(f);

I guess.

John

CBFalconer

unread,
Sep 27, 2001, 11:25:10 AM9/27/01
to
John wrote:
>
> "Titus Cheung" <ti...@ieee.org> wrote in message
> news:zvBs7.17919$ob.4...@news1.rdc1.bc.home.com...
> >
> > Is there a function in the math.h library called "round"? I just need to
> > round a double variable into an integer value, not an integer variable.
>
> float f = 1.345;
> float f1 = (int) f;
> printf("%g", f1);

That truncates. Try:

float f1, f = 1.345;

#define roundfloat(f) (int)((f) + 0.5)

f1 = roundfloat(f); /* may have range problems */

and you may be better suited with floor(), floorf(), nearbyint(),
nearbyintf(). See N869 or dinkums library reference page.

--
Chuck F (cbfal...@yahoo.com) (cbfal...@XXXXworldnet.att.net)
(Remove "XXXX" from reply address. yahoo works unmodified)
mailto:u...@ftc.gov (for spambots to harvest)


John

unread,
Sep 27, 2001, 2:57:03 PM9/27/01
to

"CBFalconer" <cbfal...@yahoo.com> wrote in message >

>
> #define roundfloat(f) (int)((f) + 0.5)
>
> f1 = roundfloat(f); /* may have range problems */

Thanks.

John

Toni Rönkkö

unread,
Sep 28, 2001, 7:13:28 AM9/28/01
to
Try this program:

/* Based on round.awk by Arnold Robbins, arn...@gnu.org, Public Domain */
int my_round (double x) {
int i = (int) x;
if (x >= 0.0) {
return ((x-i) >= 0.5) ? (i + 1) : (i);
} else {
return (-x+i >= 0.5) ? (i - 1) : (i);
}
}

#include <stdio.h>
#include <stdlib.h>
int main (void) {
printf ("my_round(-5.5) = %d\n", my_round (-5.5));
printf ("my_round(-3.6) = %d\n", my_round (-3.6));
printf ("my_round(-1.2) = %d\n", my_round (-1.2));
printf ("my_round(-0.7) = %d\n", my_round (-0.7));
printf ("my_round(-0.2) = %d\n", my_round (-0.2));
printf ("my_round( 0.2) = %d\n", my_round ( 0.2));
printf ("my_round( 0.7) = %d\n", my_round ( 0.7));
printf ("my_round( 5.5) = %d\n", my_round ( 5.5));
return EXIT_SUCCESS;
}


Running the program should produce:

my_round(-5.5) = -6
my_round(-3.6) = -4
my_round(-1.2) = -1
my_round(-0.7) = -1
my_round(-0.2) = 0
my_round( 0.2) = 0
my_round( 0.7) = 1
my_round( 5.5) = 6

There is no round() in C but HP-UX math libraries do provide a
function named `round'. If you want to compile your code on HP, name
our function differently.

Toni Ronkko

Mal Kay

unread,
Sep 28, 2001, 8:42:28 AM9/28/01
to
CBFalconer wrote:
>
> John wrote:
> >
> > "Titus Cheung" <ti...@ieee.org> wrote in message
> > news:zvBs7.17919$ob.4...@news1.rdc1.bc.home.com...
> > >
> > > Is there a function in the math.h library called "round"? I just need to
> > > round a double variable into an integer value, not an integer variable.
> >
> > float f = 1.345;
> > float f1 = (int) f;
> > printf("%g", f1);
>
> That truncates. Try:
>
> float f1, f = 1.345;
>
> #define roundfloat(f) (int)((f) + 0.5)
>
> f1 = roundfloat(f); /* may have range problems */
>

Gives quite wrong results for f negative!
(At least not the result wanted by the OP)

> and you may be better suited with floor(), floorf(), nearbyint(),
> nearbyintf(). See N869 or dinkums library reference page.
>

With the rounding mode set to requirements nearbyint() should
be the prefered method, but has only appeared with the C99 library

Malcolm Kay

Lawrence Kirby

unread,
Sep 28, 2001, 11:37:51 AM9/28/01
to
In article <vm4bsjv...@muikku.uku.fi>
tro...@muikku.uku.fi "Toni Rönkkö" writes:

...

>There is no round() in C but HP-UX math libraries do provide a
>function named `round'. If you want to compile your code on HP, name
>our function differently.

C99 does define a round() function. Of course that's not a lot of
use unless you have a C99 compiler, or at least one that implements
the C99 function.
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------

0 new messages