Well, Python's round() seems to be an asymmetrical half up algorithm, as opposed to the symmetrical half up rounding in common math. Using epsilon will not correct that. I grand you the sign issue, but not the precision issue you mentioned.
Corrected algorithm:
def roundf(f, prec=0): return round(f + cmp(f, 0.0) * (.1 / pow(10, prec + 2), prec)
>>> roundf(-2.675000000000005, 14) -2.67500000000001 >>> roundf(2.675000000000005, 14) 2.67500000000001 >>> roundf(2.675000000000005, 15) 2.675000000000005 >>> roundf((-2.675000000000005, 15) -2.675000000000005 >>> roundf(0.575,2) 0.58 >>> roundf(2.675,2) 2.68 >>> roundf(-2.678,2) -2.68
Cheers
Well, Python's round() seems to be an asymmetrical half up algorithm, as opposed to the symmetrical half up rounding in common math. Using epsilon will not correct that. I grand you the sign issue, but not the precision issue you mentioned.
Corrected algorithm:
def roundf(f, prec=0):
return round(f + cmp(f, 0.0) * (.1 / pow(10, prec + 2), prec)
>>> roundf( -2.675000000000 005, 14) 2.6750000000000 05, 14) 2.6750000000000 05, 15) (-2.67500000000 0005, 15)
-2.67500000000001
>>> roundf(
2.67500000000001
>>> roundf(
2.675000000000005
>>> roundf(
-2.675000000000005
>>> roundf(0.575,2)
0.58
>>> roundf(2.675,2)
2.68
>>> roundf(-2.678,2)
-2.68
Cheers