Skip to main content\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 18.10 Rounding in C
C supports several functions to help with rounding (such as rounding up, rounding down, just rounding, etc). You need to
#include <math.h>
in order to use them. Here are some of the functions at your disposal:
Table 18.1. Rounding Functions in C
| Function |
Prototype |
Description |
floor() |
double floor(double x); |
Returns the nearest integer which is less than or equal to the argument passed to this function.
|
round() |
double round (double x); |
Returns the nearest integer value of the argument passed to this function.
|
lround() |
long lround (double x); |
Returns the nearest integer value of the argument passed to this function.
|
ceil() |
double ceil (double x); |
Returns the nearest integer value which is greater than or equal to the argument passed to this function.
|
Activity 18.4.
Why might it be useful to have both a
round() and an
lround() function?