> wyvq` -bjbjqPqP .T::%''''D'Tq2$($($($($($($($(qqqqqqq$rh+ud6q$($($($($(6q$($(Kq,,,$(r$($(q,$(q,,jkp$((R1')m2qaq0qmu+<udpup$($(,$($($($($(6q6q,
$($($(q$($($($(|#|#THE ART OF ROUNDING
===================
by Edward F. Moneo, 27-Nov-2007
This tutorial includes coded algorithms in Microsoft QuickBasic 4.5, and therefore the code may not apply
to other programming languages.
The term rounding encompasses several methods for converting numbers to a lesser precision.
Rounding methods are also referred to as rounding modes or rounding rules.
Some reasons for performing rounding are:
Converting numbers with decimals to whole numbers.
Converting numbers with three or more decimal places to dollars and cents with two decimal places.
Converting numbers to a workable value, like PI of 3.14159265 to 3.1416.
Converting numbers to tens, hundreds, thousands, etc. for reports or charts.
Technical Definition of Rounding:
To adjust numbers to fit a format using a rounding mode.
The FORMAT determines how the rounded result appears.
Numbers with N number of decimal places, where N can be zero.
Numbers expressed as hundreds, thousands, millions, etc.
The ROUNDING MODE determines whether to round up or down, truncate,
how to handle midway digits like .5, and how to handle negative numbers.
The programmer decides on the FORMAT to be used, and performs the required ROUNDING MODE algorithm.
Depending on the FORMAT, the programmer may need to scale the number before and after invoking the algorithm.
ROUNDING TERMINOLOGY:
ROUND-TOWARD-ZERO:
[-Infinity] ----(negatives)----> 0 <----(positives)---- [+Infinity]
Negatives get rounded toward zero, i.e., to a greater negative number.
Positives get rounded toward zero, i.e., to a lesser positive number.
ROUND-AWAY-FROM-ZERO:
[-Infinity] <----(negatives)---- 0 ----(positives)----> [+Infinity]
Negatives get rounded away from zero, i.e., to a lesser negative number.
Positives get rounded away from zero, i.e., to a greater positive number.
ROUND-TOWARDS-POSITIVE-INFINITY:
[-Infinity] ----(negatives)----> 0 ----(positives)----> [+Infinity]
Both negative and positive numbers are rounded towards positive infinity.
Expressed with reference to zero:
Negatives get rounded toward zero, i.e., to a greater negative number.
Positives get rounded away from zero, i.e., to a greater positive number.
ROUND-TOWARDS-NEGATIVE-INFINITY:
[-Infinity] <----(negatives)---- 0 <----(positives)---- [+Infinity]
Both negative and positive numbers are rounded towards negative infinity.
Expressed with reference to zero:
Negatives get rounded away from zero, i.e., to a lesser negative number.
Positives get rounded toward zero, i.e., to a lesser positive number.
ROUNDING MODES AND ALGORITHMS:
ROUND-TOWARD-NEAREST:
You will hear Round-Toward-Nearest or Round-To-Nearest referred to as a rounding mode,
but it is only a rounding category or generic concept, and not a precise rounding algorithm.
Round-Toward/To-Nearest, as a concept, encompasses several actual rounding algorithms; such as,
the symmetric and asymmetric variations of Round-Half-Up, Round-Half-Down, Round-Half-Even, etc.
From a programming point of view, Round-Toward/To-Nearest by itself is worthless.
SYMMETRIC-ARITHMETIC-ROUNDING:
Aka Round-Half-Up-Symmetric, Half-Adjusting, Conventional Rounding.
Negative numbers, whose decimal value is greater than or equal to .5, increase towards negative infinity. Others are truncated.
Positive numbers, whose decimal value is greater than or equal to .5, increase towards positive infinity. Others are truncated.
This is the classic or conventional rounding mode which many of us learned in school.
A rounding factor of .5 is added to the number to be rounded, and the fractional part of the result is truncated.
In the case of a negative number, the same operation is performed on the absolute value of the number.
Algorithm code:
RoundedResult = SGN(Number)*INT(ABS(Number)+.5)
An alternate method:
RoundedResult = FIX(Number+.5*SGN(Number))
Examples:
Negative Values: -2.7 -2.5 -2.3 -2.0 -1.7 -1.5 -1.3 -1.0 -0.7 -0.5 -0.3
Rounded Results: -3 -3 -2 -2 -2 -2 -1 -1 -1 -1 0
Positive Values: +0.0 +0.3 +0.5 +0.7 +1.0 +1.3 +1.5 +1.7 +2.0 +2.3 +2.5 +2.7
Rounded Results: 0 0 +1 +1 +1 +1 +2 +2 +2 +2 +3 +3
ASYMMETRIC-ARITHMETIC-ROUNDING:
Aka Round-Half-Up-Asymmetric.
Both negative and positive numbers increase towards positive infinity when the decimal value is greater than or equal to .5.
Others are truncated.
Algorithm code:
RoundedResult = INT(Number+.5)
Examples:
Negative Values: -2.7 -2.5 -2.3 -2.0 -1.7 -1.5 -1.3 -1.0 -0.7 -0.5 -0.3
Rounded Results: -3 -2 -2 -2 -2 -1 -1 -1 -1 0 0
Positive Values: +0.0 +0.3 +0.5 +0.7 +1.0 +1.3 +1.5 +1.7 +2.0 +2.3 +2.5 +2.7
Rounded Results: 0 0 +1 +1 +1 +1 +2 +2 +2 +2 +3 +3
BANKERS' ROUNDING:
Aka Round-Half-Even, RoundTiesToEven, Unbiased Rounding, Convergent Rounding,
Statistician's Rounding, Round to Nearest Even, Round to Even.
Symmetrical by definition.
Some people learned this mode in school. Similar to Symmetric-Arithmetic, with the exception that halfway values of .5
are rounded towards the nearest even digit. The theory is that this more or less evenly distributes upward and downward rounds
of exact halves, which might reduce the overall error in a computation that involves many roundings.
Algorithm code:
RoundedResult = CINT(Number)
or alternate method using integer divide.
RoundedResult = Number\1
Examples:
Negative Values: -2.7 -2.5 -2.3 -2.0 -1.7 -1.5 -1.3 -1.0 -0.7 -0.5 -0.3
Rounded Results: -3 -2 -2 -2 -2 -2 -1 -1 -1 0 0
Positive Values: +0.0 +0.3 +0.5 +0.7 +1.0 +1.3 +1.5 +1.7 +2.0 +2.3 +2.5 +2.7
Rounded Results: 0 0 0 +1 +1 +1 +2 +2 +2 +2 +2 +3
ROUND-CEILING:
* Rounds towards positive infinity.
Algorithm code:
if Number < 0 then
RoundedResult=FIX(Number)
else
if Number=FIX(Number) then
RoundedResult=Number
else
RoundedResult=INT(Number)+1
end if
end if
Examples:
Negative Values: -2.7 -2.5 -2.3 -2.0 -1.7 -1.5 -1.3 -1.0 -0.7 -0.5 -0.3
Rounded Results: -2 -2 -2 -2 -1 -1 -1 -1 0 0 0
Positive Values: +0.0 +0.3 +0.5 +0.7 +1.0 +1.3 +1.5 +1.7 +2.0 +2.3 +2.5 +2.7
Rounded Results: 0 +1 +1 +1 +1 +2 +2 +2 +2 +3 +3 +3
ROUND-FLOOR:
* Rounds towards negative infinity.
Algorithm code:
RoundedResult=INT(Number)
Examples:
Negative Values: -2.7 -2.5 -2.3 -2.0 -1.7 -1.5 -1.3 -1.0 -0.7 -0.5 -0.3
Rounded Results: -3 -3 -3 -2 -2 -2 -2 -1 -1 -1 -1
Positive Values: +0.0 +0.3 +0.5 +0.7 +1.0 +1.3 +1.5 +1.7 +2.0 +2.3 +2.5 +2.7
Rounded Results: 0 0 0 0 +1 +1 +1 +1 +2 +2 +2 +2
ROUND-TOWARD-ZERO:
Aka Truncation.
This is the simplest rounding mode. All digits, usually the decimals,
beyond the number required are dropped. The result is a number whose
magnitude is always less than or equal to the original value.
For POSITIVE numbers, perform ROUND-FLOOR.
For NEGATIVE numbers, perform ROUND-CEILING.
Or, perform the following algorithm code:
RoundedResult=FIX(Number)
ROUND-AWAY-FROM-ZERO:
Aka Round toward infinities.
For POSITIVE numbers, perform ROUND-CEILING.
For NEGATIVE numbers, perform ROUND-FLOOR.
ROUND-UP:
When "UP" means heading towards positive infinity, perform ROUND-CEILING.
When "UP" means an absolute value heading away from zero,
perform ROUND-AWAY-FROM-ZERO.
ROUND-DOWN:
When "DOWN" means heading towards negative infinity, perform ROUND-FLOOR.
When "DOWN" means an absolute value heading towards zero,
perform ROUND-TOWARD-ZERO.
TRUNCATION:
Aka Chopping.
Discard any unwanted digits, usually any decimals.
Perform ROUND-TOWARD-ZERO.
OTHER OBSCURE ROUNDING MODES NOT DISCUSSED HERE.
Asymmetric Bankers' Rounding
Round-Half-Down, symmetric or asymmetric
Round-Half-Odd, symmetric or asymmetric
Round Half-Integer Away From Zero
Round-Alternate
Round-Random
Round-Toward-Nearest, ties away from zero.
Round-Toward-Nearest, ties toward zero.
QUICKBASIC CONSIDERATIONS REGARDING ROUNDING:
You cannot assume what rounding mode is used when the QuickBasic manual
says that rounding is performed by certain operators and functions. The
following are the operators and functions that specify or imply rounding:
CINT or CLNG: The manual says: "Converts a numeric expression to an integer
(or long integer) by rounding the fractional part of the expression."
Not stated, but the rounding mode used is Bankers' Rounding.
INTEGER DIVISION: The manual says: "Before integer division is performed,
operands are rounded to integers or long integers..."
Not stated, but the rounding mode used is Bankers' Rounding.
Integer Division does a 3 step operation.
1) It rounds the dividend using Bankers' Rounding.
2) It rounds the divisor using Bankers' Rounding.
3) It then divides the dividend by the divisor, and then TRUNCATES the result
toward zero. Due to the divide, a "division by zero" error is possible.
INTEGER ASSIGNMENT: While transforming the floating point expression to an
integer, it performs Bankers' Rounding.
The INT function: The manual says: "Returns the largest integer less than
or equal to the expression." The INT actually performs a Round Floor type
of Truncation, also referred to as Round Toward Negative Infinity.
PRINT USING: The manual says: "Numbers are rounded as necessary."
Not stated, but the rounding mode used is Symmetric Arithmetic Rounding.
Note: when rounding a negative number, like (-.4), Print Using will display a "-0".
*****
=>IO %
&
C
W
Z
k
AU1Pij023wx()FGUXnop&I_`eh
IhB
h#h~$>*h#h~$5>*hvFPh0$h7b!h7b!h~$5>*hqh Uxh~$huR$J(HIJ2 3 ~ ?
12h%
&Fgd4
&Fgd^ggduR$gd~$-%noA
B
C
Y
Z
m
?@AW/01RKgd~$Gk2G``
&Fgd^g
a5gdvFPgd~$eh'(ABn-/EINOTUZ[]mwz~пппппппޜޜޜޜޜޜޜޜޜޜޜޜ h5:hQ^CJOJQJ^JaJh5:h0$CJOJQJ^JaJ h5:h5:CJOJQJ^JaJh5:CJOJQJ^JaJhQ^CJOJQJ^JaJh0$hoFhB
h~$h^g=`(pvwx3IJZ`gd4gdQ^gd5:gd~$`gd7b!
&Fgd7b!
&Fgd^g%(,/0268<>Buvx23I{
"+,-ABCEIKOQYÿÿÿÿûӪӪӪӪӪӪӪӪӪӜӜӪӪӪӪӪhCJOJQJ^JaJ h5:hoFCJOJQJ^JaJhoFh
Ih~$h#h~$>*h5:hoFCJOJQJ^JaJhQ^CJOJQJ^JaJ h5:hQ^CJOJQJ^JaJ=Zz{,-A%Oijtgd&`gdI|
&FgdI|gdI|gdoFgd~$YZ[]_`acefgiklST)*uvjtɻ h5:h&CJOJQJ^JaJh&CJOJQJ^JaJh&hI|h#h~$>*hoFh~$hoFCJOJQJ^JaJ h5:hoFCJOJQJ^JaJB01248:>@HIJLNOPRTUVXZ[npt| Ͻ h5:hK%CJOJQJ^JaJh~$h#h~$>*hK%hK%CJOJQJ^JaJh&h&CJOJQJ^JaJ h5:h&CJOJQJ^JaJhy
CJOJQJ^JaJ?u&BGd~ h i !!!gdK%gd~$gd& " % ' ( - . : ? @ E F K L V X g h i } ~ !!!!!!!!'!M!N!y!!!!!!!!!h#h~$>*h~$hK%hJ/CJOJQJ^JaJ h5:hK%CJOJQJ^JaJhK%CJOJQJ^JaJM!)!M!N!^!x!y!!!*"+""""""#G#####$$N$h$i$j$`gdI|
&FgdI|gdK%gd~$!!!!!!!!!!!!!!
""" "$"&")"*"+"?"@"A"C"G"I"M"O"W"X"Y"["]"^"_"a"c"d"e"g"i"j"}"""""""""""""""""""""""""j$~$$%%ûûhZ0h~$>*h~$h#h~$>*hK%hmMCJOJQJ^JaJhK%CJOJQJ^JaJ h5:hK%CJOJQJ^JaJJj$$$$$$$%L%%%%%%%7&R&S&T&`&n&&&&&&'5'`gdI|
&FgdI|gd~$%%T&^&&&' ())))++/,?,-----hZ0hZ0hZ0>*hZ0h~$5>*h*h~$hZ0h~$>*5']'''''''''"(#(l((()M))))*R*****+n+++gd~$+,.,/,y,,--I------gd~$
,1h/ =!"#$%@@@NormalCJ_HaJmH sH tH DA@DDefault Paragraph FontRi@RTable Normal4
l4a(k@(No List%T(HIJ23~?12h%noABCYZm?@AW/01RKG k
2
G``
(pvwx3IJZz{,-A%Oijtu&BGd~hi)MN^xy*+G$NhijL7RST`n5]" # l !M!!!!"R"""""#n###$.$/$y$$%%I%%%%%%0000000000000 0 0 0 000000 0 00000000000000000000000000000000000000000000000000000000 0 0 0 0 0
00000000000000000 0 000000000000000 00 0 00000000000000000000000000000000@00@0@00000000000@00@0@00000 0 0000 0 0 00000 0 0 0000 0 00000 0 00000 0 0 000000000000000000000000000000000000000000000000pv,-uhi*+I%%%%%%K009K00
K00K0
0K00
K00K00
K00K0
0K00
K00K00
K00K0
0K00
K00K00
K00I0
0I00
K00K00
K00I0
0I00
K00K00,X9K006
sT
0
0
0eY !%- !#%'%`Z!j$5'+-"$&()-5:78Zg'(O\(5iv]^cd^kN[`c%(*`fy~ci^c
GJjn%'6:BFIKlpGM\`7>l p M!!"$"q#w#$
$y${$$$%3333333333333333333333333333333333333333CYZmpv3J-ui)N^y+j`n%%6
hH,:V{"b@pC"|) T[n ?ts^#84N-8pU-$N\H7"{2?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdeghijklmopqrstuxRoot Entry FR1z1Table+uWordDocument.TSummaryInformation(fDocumentSummaryInformation8nCompObjq
FMicrosoft Office Word Document
MSWordDocWord.Document.89q