Round-toward-nearest
This is perhaps the most intuitive of the various rounding algorithms. In this case, values such as 5.1, 5.2, 5.3, and 5.4 would round down to 5, while values of 5.6, 5.7, 5.8, and 5.9 would round up to 4. The trick, of course, is to decide what to do in the case of the half-way value 5.5. In fact, round-toward-nearest may be considered to be a superset of two complementary options known as
round-half-up and
round-half-down, each of which treats the 5.5 value in a different manner as discussed below.
Round-Half-Up (Arithmetic Rounding)
This algorithm, which may also be referred to as arithmetic rounding, is the one that we typically associate with the concept of rounding that we learned at school. In this case, a "half-way" value such as 5.5 will round up to 6.
One way to view this is that we can consider there to be ten values that commence with a "5" in the most-significant place (5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, and 5.9). On this basis, it intuitively makes sense for five of the values to round down and for the other five to round up; that is, for the five values 5.0 through 5.4 to round down to 5, and for the remaining five values 5.5 through 5.9 to round up to 6.
It’s worth noting that "half-way" values will always round up when using this algorithm, irrespective of whether the final result is odd or even.
The tricky point with this round-half-up algorithm arrives when we come to consider negative numbers. There is no problem in the case of the values like -5.1, -5.2, -5.3, and -5.4, because these will all round to the nearest integer, which is -5. Similarly, there is no problem in the case of values like -5.6, -5.7, -5.8, and -5.9, because these will all round to -6. The problem arises in the case of "half-way" values like -5.5 and -6.5 and our definition as to what
"up" means in the context of
"round-half-up." Based on the fact that positive values like +5.5 and +6.5 round up to +6 and +7, respectively, most of us would intuitively expect their negative equivalents of -5.5 and -6.5 to round to -6 and -7, respectively. In this case, we would say that our algorithm was
symmetric (with respect to zero) for positive and negative values. (Visualise negative numbers as "increasing" in size toward negative infinity).
However, some applications (and some mathematicians) would regard
"up" as referring to positive infinity. Based on this, -5.5 and -6.5 would actually round to -5 and -6, respectively, in which case we would class this as being an
asymmetric (with respect to zero) implementation of the round-half-up algorithm.
Round-Half-Down
Perhaps not surprisingly, this incarnation of the Round-Toward-Nearest algorithm acts in the opposite manner to its Round-Half-Up counterpart discussed above. In this case, "half-way" values such as 5.5 and 6.5 will round down to 5 and 6, respectively.
Once again, we run into a problem when we come to consider negative numbers, because what we do with "half-way" values depends on what we understand the term
"down" to mean. On the basis that positive values of +5.5 and +6.5 round to +5 and +6, respectively, a
symmetric implementation of the round-half-down algorithm will round values of -5.5 and -6.5 to -5 and -6, respectively.
By comparison, in the case of an
asymmetric implementation of the algorithm, in which
"down" is understood to refer to negative infinity, values of -5.5 and -6.5 will actually be rounded to -6 and -7, respectively.
It should be noted that a
symmetric implementation of the round-half-up algorithm acts in the same manner as an
asymmetric implementation of the round-half-down algorithm and an
asymmetric implementation of the round-half-up algorithm acts in the same manner a
symmetric implementation of the round-half-down algorithm.
Having said all that, consider the following:
OBese87’s function (a
symmetric implementation of the round-half-up algorithm)
FUNCTION _Round(n#)
dec# = n# - int(n#)
n = n# + dec#
ENDFUNCTION n
and the same function using an if/else/endif loop (an
asymmetric implementation of the round-half-up algorithm)
FUNCTION _Round(n#)
dec# = n# - int(n#)
If dec#>=-0.6
n=int(n#)
else
n = n# + dec#
endif
ENDFUNCTION n
The Wizard