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
float f = 1.345;
float f1 = (int) f;
printf("%g", f1);
John
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
>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
After reading Mal Kay's response and quote, you should probably stick
with floor() for rounding down :-)
Russ
Or using
f1 = ceil(f);
I guess.
John
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)
Thanks.
John
/* 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
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
...
>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
-----------------------------------------