MathLib.round()

MathLib.round() rounds the argument to the nearest integer. \[ \text{MathLib.round}(x) = \lfloor x + 0.5 \rfloor \]

Specification

MathLib.ceil accepts one argument. Any additional arguments will be ignored. The type of the argument should be number. For other argument types see the unary function specification.
  1. MathLib.round(NaN) = NaN
  2. MathLib.round(x) = +0 if +0 ≤ x < 0.5
  3. MathLib.round(x) = -0 if -0.5 ≤ x ≤ -0
  4. MathLib.round(+∞) = +∞
  5. MathLib.round(-∞) = -∞
  6. otherwise MathLib.round(x) = ⎣x+0.5⎦

Implementation

Due to a bug in some implementations where Math.round(-0) = +0 (and not -0), MathLib handles this special case separately. In all other cases the built-in JavaScript function Math.round is used.

Possible issues

MathLib uses the "round half up" principle: any number with fractional part 0.5 will be rounded to the next bigger integer. The rounding method is therefore asymmetric and causes a positive bias.

Tests

Categories: unary Function