There are different ways to round numbers. The way that Python does it is far more accurate than using the simple method you learned in the 5th grade. When processing large amounts of data, accuracy is important. The program below generates 10,000,000 random samples from 0 to 1000 in steps of .25. It rounds the numbers to the nearest integer, first by ties rounding away from zero (as done in the OP) and then ties rounding towards even (as Python does it). Then it prints the average of both methods, and then the error between the accumulated total of rounded values and the true total of unrounded value. The closer to 0 these values are, the more accurate the result. The program might take a few minutes depending on the speed of your computer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import random
real_total = 0.0
round_away = 0.0
round_even = 0.0
sample_size = 10000000
for i in range ( 0 , sample_size):
number = random.randrange( 0 , 4000 ) / 4
real_total + = number
round_away + = int (number + 0.5 )
round_even + = round (number)
avg_away = round_away / sample_size
avg_even = round_even / sample_size
error_away = round_away - real_total
error_even = round_even - real_total
print ( "average using round away from zero = " ,avg_away)
print ( "average using round toward even = " ,avg_even)
print ( "error using round away from zero =" ,error_away)
print ( "error using round toward even =" ,error_even)
|
Output:
average using round away from zero = 500.1346217
average using round toward even = 500.0095575
error using round away from zero = 1249678.25
error using round toward even = -963.75
In ties away from zero, you have four difference possibilities.
Output:
decimal value | .00| .25| .50| .75
difference | 0|-.25|+.50|+.25
So the average error would be (0-.25+.50+.25)/4.0 = +.125, which, over 10,000,000 samples will produce an overage of approximately 1250000, very close to the error reported by our program.
In ties toward even, you have eight different possibilities
Output:
E = even integer, O = Odd integer
decimal value |E.00|O.00|E.25|O.25|E.50|O.50|E.75|O.75
difference | 0| 0|-.25|-.25|-.50|+.50|+.25|+.25
The average error would be (0+0-.25-.25-.50+.50+.25+.25)/8 = 0, so the errors will most of the time cancel each other out and give you an error close to 0. In the run above, the error was short by 963.75, that is .00096375%, very close to 0.