Rounding functions: Difference between revisions

Content deleted Content added
mNo edit summary
moving material to significant figure; redirecting
Line 1:
#REDIRECT [[significant figure]]
To round a fractional number to the nearest integer (in C):
 
int round(double d) { return (int)(d + 0.5); }
 
The above, however, rounds negative numbers
unintuitively, e.g., -2.7 becomes -2 instead of -3.
At the expense of more computation, one can use:
 
int sround(double d) { return (int)(d + (d >= 0? 0.5 : -0.5)); }