I have recently learned about one of the main limitations of floating points: The fact that some numbers can not be represented properly in binary and might therefore give answers which are not accurate enough for your purpose.
Knowing that round(2.675, 2)
and round(2.665, 2)
both equal 2.67
I tried to write some code that would give a list of numbers that have this property (being rounded improperly).
See my code below or in this replit: https://repl.it/@FTBlover123/Float-Rounding-Test
Test = True
number1 = 0.005
number2 = 0.015
count = 0
count_per_test = 5
while Test:
if round(number1, 2) == round(number2, 2):
print number1, number2, round(number1, 2), round(number2, 2)
count += 1
else:
pass
number1 += 0.005
number2 += 0.005
if count == count_per_test:
answer = raw_input("End Program? Y / N: ")
if answer == "Y":
print "Program Ended"
Test = False
elif answer == "N":
print "Searching For %s more rounding errors" % (count_per_test)
count = 0
else:
print "Error, raw_input incorrect"
Test = False
#2.675 is a known number with a rounding error.
print 2.665, 2.675, round(2.675, 2), round(2.665, 2)
#79.705 should have one according to the results, but it doesn't truly.
print 79.695, 79.705, round(79.695, 2), round(79.705, 2)
The final 2 print
s are an example of one.
Though the code indeed seems to return some rounding errors due to the limitations of float, these seem to be non-true values (except for the first one).
This is because I am using the float 0.005 (which can not be expressed in binary) which itself causes the new number1
and number2
to be the incorrect version of its already inaccurate self! This is easily demonstrated by incrementing number1
and number2
by 0.001 (which can also not be expressed in binary), as this also results in answers which are still incorrect, but also different!
Thus, my questions:
Is it possible to create code that checks for the rounding errors due to float limitations correctly, without it suffering from the exact flaw we're trying to test for? (Though a library of such examples would also be appreciated I am much more interested in fixing the code)
The separation between my incorrect outputs seems to increase somewhat exponentially.
0.005 0.015 0.01 0.01
0.035 0.045 0.04 0.04
1.025 1.035 1.03 1.03
21.535 21.545 21.54 21.54
79.695 79.705 79.7 79.7
9164.075 9164.085 9164.08 9164.08
36933.455 36933.465 36933.46 36933.46
What causes this? Is this also true for the correct incorrectly rounded floats?