Skip to content
Advertisement

Tag: fractions

Rounding a rational number to the nearest integer, with half-up

Given a rational number, I need to get the nearest integer, with exact halves rounded up (that is, towards positive infinity). The results must be exact, which rules out float and Decimal, both of which have finite precision. The following code works perfectly in all cases except for negative numbers: For instance, it succeeds on: (5, 3) → 2 (4,

Advertisement