Rounding, floor and ceiling

Rational numbers

Algorithm X.1: The integer component of a rational number

Input: a rational number $q$.
Output: an integer

Given a rational number $\frac{a}{b}$, assumed to be in lowest terms, describe an algorithm for writing this rational number in the form $n + \frac{c}{b}$ where $0 \leq c < b$ when the rational number is positive, and in the form $n - \frac{c}{b}$ when the rational number is negative.

Algorithm X.2: Floor of a rational number

Input: a rational number $q$.
Output: an integer

Write an algorithm for finding the floor of a rational number; that is, the greatest integer less than or equal to the given rational number.

Algorithm X.3: Ceiling of a rational number

Input: a rational number $q$.
Output: an integer

Write an algorithm for finding the ceiling of a rational number; that is, the smallest integer greater than or equal to the given rational number.

Algorithm X.4: Truncation of a rational number

Input: a rational number $q$.
Output: an integer

Write an algorith to truncate a rational number; that is, the closest integer in absolute value that falls between $0$ and the given rational number.

Algorithm X.5: Rounding of a rational number

Input: a rational number $q$.
Output: an integer

Write an algorithm to round a rational number; that is find the closest integer. If two integers are equally close, round to the closest even integer.

Real numbers

Given a non-zero real number in scientific notation in the form $m.mmm\cdots \times 10^e$, this number represenents $n$ significant digits if there are $n - 1$ digits in the mantissa ($.mmm\cdots$). Alternatively, the digits $m.mmm\cdots$ are also described as the significand, and the real number has $n$ significant digits if there are $n$ digits in the significand $m.mmm\cdots$.

Algorithm X.6: The integer component of a real number

Algorithm X.7: Floor of a real number

Algorithm X.8: Ceiling of a real number

Algorithm X.9: Truncation of a real number to $n$ significant digits

Algorithm X.10: Rounding of a real number to $n$ significant digits

Input: a real number $x$ in its decimal form.
Output: an integer